我正在尝试从我的Excel文件中选择“报告详细信息”工作表。但是,我在选择它时遇到了麻烦,
Microsoft Jet数据库引擎找不到对象“报告详细信息”。确保对象存在,并且您正确拼写其名称和路径名称。
if (fileExtension == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (fileExtension == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection con = new OleDbConnection(connString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
cmd.CommandText = "SELECT * FROM [Report Details]"; //ERROR HERE
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);
con.Close();
GridView1.DataSource = dtExcelRecords;
GridView1.DataBind();
在数据集查看器中查看我的“Tables”,连接字符串能够访问该文件。 “报告详细信息”列显示为“报告详细信息$”。我试过这样输入,但我仍然收到错误。
答案 0 :(得分:2)
您的问题标题为EXCEL/C# Cant Find Worksheet
你的帖子说I am trying to select the "Report Details" WORKSHEET from my excel file.
您收到的错误是
The Microsoft Jet database engine could not find the object 'Report Details'. Make sure the object exists and that you spell its name and the path name correctly.
<强>解决方案强>
您错过了$
标志
尝试此操作(已完成测试)
cmd.CommandText = "SELECT * FROM [Report Details$]";
答案 1 :(得分:0)
我能够通过dtExcelSheetName
获取表名cmd.CommandText = "SELECT * FROM [" + con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[1][2].ToString() + "]";
现在正在运作。