ASP.NET Upload Excel and Import to Database (Excel Application) |
ASP.NET Upload Excel and Import to Database (Excel Application) ตัวอย่างนี้จะเป็นการเขียน Script เพื่อทำการ Upload ไฟล์ Excel (*.xls) แล้วทำการอัพโหลดข้อมูล ที่ได้จากการอัพโหลดเข้าสู่ฐานข้อมูลโดยผ่านคำสั่ง INSERT INTO
Framework 1.1,2.0,3.0,4,0
Excel for C# อ่านและดัดแปลงได้จากบทความนี้
Syntax
Dim xlApp As New Excel.Application
AspNetUploadExcelDatabase.aspx
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.OleDb"%>
<%@ Import Namespace="Excel"%>
<%@ Page Language="VB" %>
<script language="VB" runat="server">
Sub btnUpload_OnClick(sender As Object, e As EventArgs)
If Not IsNothing(myFile1.PostedFile) Then
Dim UlFileName As String
Dim i As Integer
UlFileName = "MyXls/" & System.IO.Path.GetFileName(myFile1.Value)
'*** Save Images ***'
myFile1.PostedFile.SaveAs(Server.MapPath(UlFileName))
'*** Create Excel.Application ***'
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet1 As Excel.Worksheet
xlBook = xlApp.Workbooks.Open(Server.MapPath(UlFileName))
xlBook.Application.Visible = False
xlSheet1 = xlBook.Worksheets(1)
'*** Create DataTable ***'
Dim dt As New System.Data.DataTable
Dim dr As System.Data.DataRow
'*** Column ***'
dt.Columns.Add("CustomerID")
dt.Columns.Add("Name")
dt.Columns.Add("Email")
dt.Columns.Add("CountryCode")
dt.Columns.Add("Budget")
dt.Columns.Add("Used")
i = 2
Do While Not Trim(xlSheet1.Cells.Item(i, 1).Value) = ""
'*** Rows ***'
dr = dt.NewRow
dr("CustomerID") = xlSheet1.Cells.Item(i, 1).Value
dr("Name") = xlSheet1.Cells.Item(i, 2).Value
dr("Email") = xlSheet1.Cells.Item(i, 3).Value
dr("CountryCode") = xlSheet1.Cells.Item(i, 4).Value
dr("Budget") = xlSheet1.Cells.Item(i, 5).Value
dr("Used") = xlSheet1.Cells.Item(i, 6).Value
dt.Rows.Add(dr)
i = i + 1
Loop
'*** End DataTable ***'
'*** Insert to Database ***'
Dim objConn As OleDbConnection
Dim objCmd As OleDbCommand
Dim strConnString As String
Dim strSQL As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";"
objConn = New OleDbConnection(strConnString)
objConn.Open()
For i = 0 To dt.Rows.Count - 1 '*** Insert Record ***'
strSQL = "INSERT INTO customer2 (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
" VALUES ('" & dt.Rows(i)("CustomerID") & "','" & dt.Rows(i)("Name") & "' " & _
",'" & dt.Rows(i)("Email") & "','" & dt.Rows(i)("CountryCode") & "' " & _
",'" & dt.Rows(i)("Budget") & "','" & dt.Rows(i)("Used") & "') "
objCmd = New OleDbCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
Next
Me.lblText.Text = "Record Inserted."
'*** Quit and Clear Object ***'
objConn.Close()
objConn = Nothing
xlApp.Application.Quit()
xlApp.Quit()
xlSheet1 = Nothing
xlBook = Nothing
xlApp = Nothing
End IF
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com ASP.NET - Excel Application</title>
</head>
<body>
<form id="form1" runat="server">
<input id="myFile1" type="file" runat="server">
<input id="btnUpload" type="button" OnServerClick="btnUpload_OnClick" value="Upload" runat="server" />
<hr />
<asp:Label id="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
</form>
Screenshot
|