1
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)然后它炸弹说:

*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!我在这里做错什么了吗?

谢谢你的帮助!

4

1 回答 1

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,但这没关系

于 2013-05-03T15:58:22.153 回答