web 2.0

ASP.NET(vb.net) & Upload excel import to database (Excel.Application)

ASP.NET(vb.net) & Upload excel import to database (Excel.Application) - This article example scripts you will learn how to Upload excel file and import in to database using ASP.NET Scripts.

ShotDev Focus:
- ASP.NET(vb.net) & Upload excel import to database (Excel.Application)

Example

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>ShotDev.Com Tutorial</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>

Screenshot

ASP.NET(vb.net) & Upload excel import to database (Excel.Application)

ASP.NET(vb.net) & Upload excel import to database (Excel.Application)
.
.
.
Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.