Paul Clement
9/16/2002 9:10:00 PM
On Mon, 16 Sep 2002 15:46:35 -0400, "Murali" <cutekutty@hotmail.com> wrote:
¤ Hi
¤
¤ How to convert Excel file into table using VB.NET code (not in ADO)?
¤
I don't know what kind of table you are referring to, but the following code will read an Excel
Worksheet into a DataTable:
'Establish a connection to the data source.
Dim sConnectionString As String
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=d:\My Documents\Book2.xls;" & _
"Extended Properties=""Excel 8.0;HDR=Yes"""
Dim objConn As New System.Data.OleDb.OleDbConnection(sConnectionString)
objConn.Open()
Dim da As New System.Data.OleDb.OleDbDataAdapter("Select * from [Sheet4$]", objConn)
Dim ds As New DataSet("ExcelDS")
da.Fill(ds, "ExcelSheet")
Dim dt As DataTable
dt = ds.Tables("ExcelSheet")
Dim drCurrent As DataRow
For Each drCurrent In dt.Rows
Console.WriteLine("{0} {1}", _
drCurrent("Col1").ToString, _
drCurrent("Col2").ToString)
Next
objConn.Close()
If the first row of the Excel Worksheet does not contain column names then remove the HDR=Yes
Extended Property from the connection string.
Paul ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)