2013-05-03 54 views
0
private void readXLSData() 
    { 
     OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\nsharifzadeh\\Desktop\\Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;"""); 
     con.Open(); 
     OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 
    } 

我使用的調試,似乎所有的方式進行合作,以da.Fill(ds)然後彈說:如何使用OleDbConnection類(或其他)讀取XLS文件?

*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.*

我發誓表的名稱是工作表Sheet1!有什麼我在這裏做錯了?

感謝您的幫助!

+0

有何工作表的名稱? (在標籤上),它是工作表,還是你重命名了它? – Derek 2013-05-03 15:53:02

回答

1

一兩件事你可以嘗試,有時工作表名稱需要'周圍......像「Sheeet1 $」

OleDbDataAdapter da = new OleDbDataAdapter("select * from ['Sheet1$']", con); 

如果不工作。試試你的連接。

DataTable schemaTable = con.GetSchema("TABLES"); 
foreach (DataRow dataRow in con.Rows) 
{ 
    //tablename = rowData[2] check those contents to see the sheet names in the excel spreadsheet. 
} 

現在,我一直在用Excel 12.0和Microsoft.ACE.OLEDB.12.0,但是這不應該的問題

相關問題