代码示例:
Using spreadsheetDocument__1 As SpreadsheetDocument = SpreadsheetDocument.Open(path, False) Dim workbookPart As WorkbookPart = spreadsheetDocument__1.WorkbookPart Dim sheet As Sheet = workbookPart.Workbook.Descendants(Of Sheet)().FirstOrDefault() Dim worksheet As Worksheet = DirectCast(workbookPart.GetPartById(sheet.Id.Value), WorksheetPart).Worksheet
For Each row As Row In worksheet.Descendants(Of Row)()
For Each cell As Cell In row.Descendants(Of Cell)()
' Do something with the cell value here...
Next
Next
End Using
代码示例:
Dim connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & path & ";Extended Properties=Excel 12.0;" Dim adapter As New OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString) Dim ds As New DataSet() adapter.Fill(ds)
' Use the dataset here...
但此方法仅适用于2007及以上版本的Excel文件。如需使用2003及以下版本的Excel文件,需要将连接字符串中的“Excel 12.0”更改为“Excel 8.0”。