2012-04-13 47 views
8

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ó?

+0

thử một lần sử dụng đường dẫn absoulte: Nguồn dữ liệu = C: \\ myexcel.xls; –

+0

@AshwiniVerma 'filepath' là đường dẫn tuyệt đối bởi vì tôi sử dụng' Server.MapPath() ' – Curt

+0

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 –

Trả lời

13

Tôi đã tìm thấy sự cố.

Dường như bảng tính đã được lưu vào vị trí sai, do đó, filepath không được trỏ đến tệp tồn tại.

Tôi không kiểm tra điều này lúc đầu vì tôi cho rằng một thông báo lỗi khác sẽ xuất hiện. Không thể tìm thấy một cái gì đó như "Book1.xls". Tuy nhiên nó có vẻ như nếu nó không tồn tại, sau đó thông báo sẽ chỉ ra rằng nó không thể tìm thấy Worksheet.

0

Không chắc chắn, tôi có một số mã tương tự (C#) hoạt động tốt ...

Có thể bạn có thể phát hiện sự khác biệt?

string connectionString = string.Format(Thread.CurrentThread.CurrentCulture, "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=YES;'", excelFilePath); 
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); 
using (DbConnection connection = factory.CreateConnection()) 
{ 
    connection.ConnectionString = connectionString; 
    using (DbCommand command = connection.CreateCommand()) 
    { 
     command.CommandText = @"SELECT [File], [ItemName], [ItemDescription], [Photographer name], [Date], [Environment site] FROM [Metadata$]"; 
     connection.Open(); 
     using (DbDataReader dr = command.ExecuteReader()) 
     { 
      if (dr.HasRows) 
      { 
       while (dr.Read()) 
       { 
        ....... 
       } 
      } 
     } 
     connection.Close(); 
    } 
} 

Hãy thử đổi tên trang tính của bạn; hoặc thêm cột rõ ràng; hoặc kiểm tra xem nó có phân biệt chữ hoa chữ thường hay không.

0

Thay đổi vị trí tệp Excel của bạn, lỗi này sẽ được giải quyết. có thể đặt tập tin của bạn trong cùng một thư mục nơi nguồn của bạn hiện tại

0

giải pháp tốt nhất thông qua vb mã từ liên kết này, tất cả các khoản tín dụng đối với các giải pháp dự kiến ​​của tôi folks- http://www.vbforums.com/showthread.php?507099-data-from-excel-sheet-to-datagrid-(vb)

C# bên dưới

string connString = "Driver={Microsoft Excel Driver (*.xls)};READONLY=FALSE;DriverId=790;Dbq=" + "C:\\Users\\BHARAVI\\Documents\\visual studio 2013\\Projects\\ERP\\ERPAutomation\\Assets\\Data\\Data.xls"; 

OdbcConnection conn = new OdbcConnection(connString); 

conn.ConnectionTimeout = 500; 
OdbcCommand CMD = new OdbcCommand("SELECT * FROM [Sheet1$]", conn); 
OdbcDataAdapter myDataAdaptor = new OdbcDataAdapter(CMD); 
DataSet ds = new DataSet(); 
myDataAdaptor.Fill(ds ,"Sheet1"); 
DataTable dt = ds.Tables[0]; 
foreach (DataRow dr in dt.Rows) 
{ 
    loginId = dr["LoginId"].ToString(); 
    encryptedPassword = dr["PWD"].ToString(); 
    URL = dr["URL"].ToString(); 
} 
1

Ngoài ra - hãy đảm bảo bạn chưa mở tệp trong Excel. Bạn sẽ không thể đọc tệp nếu nó mở ở một nơi khác. Tôi đã có lỗi tương tự và nhận ra rằng tôi đã mở tệp trong Excel.

0

Nếu tên file có thêm dot nhân vật như dưới đây:

sample.data.csv 

tiếp theo lựa chọn công bố:

SELECT * FROM [sample.data.csv] 

với chuỗi kết nối:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source="C:\Data\"; Extended Properties="text;HDR=Yes;Format=Delimited;"; 

sẽ thất bại với ngoại lệ:

Additional information: The Microsoft Jet database engine could not find the object 'sample.data.csv'. Make sure the object exists and that you spell its name and the path name correctly. 
Các vấn đề liên quan