Tôi đang cố gắng để đọc một tập tin bảng tính được gọi là Book1.xls
trong đó có một bảng gọi là Sheet1
Tuy nhiên tôi nhận được lỗi sau:
The Microsoft Jet database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.
Dưới đây là một đoạn mã tôi đang sử dụng:
Dim dt As DataTable = New DataTable()
Select Case fileExt
Case ".csv"
Dim reader As New CsvReader
dt = reader.GetDataTable(filePath)
Case ".xls", ".xlsx"
Dim oleDbConnStr As String
Select Case fileExt
Case ".xls"
oleDbConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filePath & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=2"""
Case ".xlsx"
oleDbConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & filePath & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
End Select
Using oleDbConn As OleDbConnection = New OleDbConnection(oleDbConnStr)
oleDbConn.Open()
Dim oleDbCmd As New OleDbCommand("SELECT * FROM [Sheet1$]", oleDbConn)
Dim oleDbDa As New OleDbDataAdapter(oleDbCmd)
oleDbDa.Fill(dt)
oleDbConn.Close()
End Using
End Select
tôi không thể hiểu tại sao đoạn code không thể tìm thấy bảng của tôi. Tại sao điều này, và làm thế nào tôi có thể giải quyết nó?
thử một lần sử dụng đường dẫn absoulte: Nguồn dữ liệu = C: \\ myexcel.xls; –
@AshwiniVerma 'filepath' là đường dẫn tuyệt đối bởi vì tôi sử dụng' Server.MapPath() ' – Curt
truy cập liên kết này và thử lấy tên sheet bằng cách lập trình: http://forums.asp.net/t/1751143.aspx/1 –