Get worksheets from Excel

  • Thread starter Thread starter Mircea Pleteriu
  • Start date Start date
M

Mircea Pleteriu

Hi,

I use the OleDb objects to connect to an excel file and retrieve data from a
specified worsheet.

What SELECT statement should I use (if there is an existing one) to retrieve
the names of the worksheets in the file?

Thanks,
Mircea
 
¤ Hi,
¤
¤ I use the OleDb objects to connect to an excel file and retrieve data from a
¤ specified worsheet.
¤
¤ What SELECT statement should I use (if there is an existing one) to retrieve
¤ the names of the worksheets in the file?
¤

Try the following:

Public Function ListExcelTables() As Boolean

Dim ExcelConnection As System.Data.OleDb.OleDbConnection
Dim ExcelTables 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()

ExcelTables =
ExcelConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing,
Nothing, Nothing, "TABLE"})

Dim RowCount As Int32
For RowCount = 0 To ExcelTables.Rows.Count - 1
Console.WriteLine(ExcelTables.Rows(RowCount)!TABLE_NAME.ToString)
Next RowCount

'frmMain.DataGrid1.DataSource = ExcelTables

Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
ExcelConnection.Close()

End Try

End Function


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Back
Top