* Reading All Excel sheet names and adding multiple sheets into single dataset with table names as sheet names.*
'Global variables
Dim excelSheetNames As String()
Dim DtSet As System.Data.DataSet = New DataSet()
Private Sub btnLoadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadData.Click
Dim MyConnection As OleDbConnection
Dim da As System.Data.OleDb.OleDbDataAdapter
Dim i As Integer
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;
data source=SStatus.xls;Extended Properties=""Excel 8.0;HDR=NO;IMEX=1"" ")
'following method gets all the Excel sheet names in the gloabal array excelSheetNames
GetExcelSheetNames("SStatus.xls")
For Each str As String In excelSheetNames
da = New OleDbDataAdapter("select * from [" & str & "]", MyConnection)
da.Fill(DtSet, excelSheetNames(i))
i += 1
Next
DataGridView1.DataSource = DtSet.Tables(0)
End Sub
Public Function GetExcelSheetNames(ByVal excelFileName As String)
Dim con As OleDbConnection = Nothing
Dim dt As DataTable = Nothing
Dim conStr As String = ("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=") + excelFileName & ";Extended Properties=Excel 8.0;"
con = New OleDbConnection(conStr)
con.Open()
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
excelSheetNames = New String(dt.Rows.Count - 1) {}
Dim i As Integer = 0
For Each row As DataRow In dt.Rows
excelSheetNames(i) = row("TABLE_NAME").ToString()
i += 1
Next
End Function