Search Here

MyLot Discussion

Friday, June 12, 2009

Import Excel File to Dataset (ASP.NET)

Dim filename as string
filename = Mid(uplFile.PostedFile.FileName, InStrRev(uplFile.PostedFile.FileName, "\") + 1, Len(uplFile.PostedFile.FileName) - InStrRev(uplFile.PostedFile.FileName, "\"))

'uplFile is ASP.NET Upload Component uplFile.PostedFile.SaveAs(System.AppDomain.CurrentDomain.BaseDirectory & filename)


Dim dbconn As New OleDb.OleDbConnection
Dim strconn As String
strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.AppDomain.CurrentDomain.BaseDirectory & filename & ";Extended Properties=Excel 8.0"
dbconn.ConnectionString = strconn
dbconn.Open()


Dim excelcmd As New OleDb.OleDbCommand
excelcmd.Connection = dbconn
excelcmd.CommandText = "select * from [Sheet1$]"


Dim excelda As New OleDb.OleDbDataAdapter(excelcmd)
Dim excelDs As New DataSet
excelda.Fill(excelDs)

0 Comments: