找不到工作表

我试图从我的Excel文件中select“报告详细信息”工作表。 不过,我在select它时遇到了麻烦,

Microsoft Jet数据库引擎找不到对象的“报告详细信息”。 确保对象存在,并且正确拼写其名称和path名称。

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(); 

在数据集查看器中查看我的“表”,连接string可以访问文件。 “报告详细信息”列显示为“报告详细信息$”。 我试图以这种方式进入,但我仍然得到一个错误。

你的问题标题说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.

您缺less$符号

试试这个( TRIED AND TESTED

 cmd.CommandText = "SELECT * FROM [Report Details$]"; 

我能够通过dtExcelSheetName获取tabel名称

 cmd.CommandText = "SELECT * FROM [" + con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[1][2].ToString() + "]"; 

现在正在工作。