private void readXLSData()
{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Usersnsharifzadeh\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)
,然后它爆炸说:
*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.*
我发誓Sheet的名字是Sheet1!我做错了什么吗?
谢谢你的帮助!
您可以尝试的一件事,有时表单名称需要"围绕它…"美元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,但这应该无关紧要