*讀所有的Excel工作表名稱和添加多個片材與表名如表名稱單個數據集*
「全局變量
昏暗excelSheetNames作爲字符串()
昏暗DtSet作爲System.Data.DataSet中=新數據集()
私人小組btnLoadData_Click(BYVAL發件人爲System.Object的,BYVALË作爲System.EventArgs)把手btnLoadData.Click
昏暗MyConnection的作爲的OleDbConnection
昏暗噠作爲System.Data.OleDb.OleDbDataAdapter
Dim i As Integer
MyConnection = New System.Data.OleDb.OleDbConnection(「provider = Microsoft.Jet.OLEDB.4.0;
數據源= SStatus.xls;擴展屬性= 「」 的Excel 8.0; HDR = NO; IMEX = 1 「」「)
「下面的方法獲取所有在gloabal陣列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作爲字符串)
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