
How to get list of Excel Sheets?
Hi there!
I'm reading Excel 2000-files using Microsoft.Jet.OLEDB.4.0 provider in my
connection string, and it works fine (if sheet named as Sheet1$). Anyway I
need to know sheet-names of the selected Excel-file, and then it makes
possible to select correct sheet of the list to the SQL Select-query, so
please let me know how can I get sheet names? Or is there better way to read
Excel-files into VB.NET 2003 Windows Forms Datagrid?
The following code should get you started:
Public Function ListExcelTablesNET() As Boolean
Dim ExcelConnection As System.Data.OleDb.OleDbConnection
Dim ExcelTable As DataTable
Try
ExcelConnection = New
System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=e:\My Documents\Book10.xls;Extended Properties=Excel 8.0;")
ExcelConnection.Open()
ExcelTable =
ExcelConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing,
Nothing, Nothing, "TABLE"})
frmMain.DataGrid1.DataSource = ExcelTable
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
ExcelConnection.Close()
End Try
End Function
Microsoft MVP (Visual Basic)