* 读取所有 Excel 工作表名称并将多个工作表添加到单个数据集中,表名作为工作表名称。*
'全局变量
将 excelSheetNames 调暗为 String()
Dim DtSet As System.Data.DataSet = New DataSet()
Private Sub btnLoadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 btnLoadData.Click
将 MyConnection 调暗为 OleDbConnection
Dim da As System.Data.OleDb.OleDbDataAdapter
将 i 调暗为整数
MyConnection = 新 System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;
数据源=SStatus.xls;扩展属性=""Excel 8.0;HDR=NO;IMEX=1""")
'以下方法获取全局数组 excelSheetNames 中的所有 Excel 工作表名称
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
公共函数 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