* Read all Excel sheet names and add multiple sheets to a single data set with table names in the form of sheet names. *
'' Global variables
Dim excelSheetNames As String ()
Dim DtSet As System.Data.DataSet = New DataSet ()
Private Sub btnLoadData_Click (sender ByVal As System.Object, ByVal e As System.EventArgs) Handles btnLoadData.Click
Dim MyConnection As OleDbConnection
Dim da As System.Data.OleDb.OleDbDataAdapter
Dim I'm As Integer
MyConnection = New System.Data.OleDb.OleDbConnection ("provider = Microsoft.Jet.OLEDB.4.0;
data source = SStatus.xls; Advanced Properties = "Excel 8.0; HDR = NO; IMEX = 1" "")
', the following method gets all the Excel sheet names in the globabal excelSheetNames array
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
Pompanagouda
source share