[英]The Microsoft Jet database engine could not find the object 'Sheet1$_'
I am reading data from an Excel file. 我正在从Excel文件读取数据。 when I read the normal Excel file,It works fine but when I read an excel file which has columns like shown below it does not find the work sheet and gives an exception-
当我阅读普通的Excel文件时,它可以正常工作,但是当我阅读具有以下所示列的Excel文件时,找不到工作表并给出异常-
The Microsoft Jet database engine could not find the object 'Sheet1$_'.
Microsoft Jet数据库引擎找不到对象“ Sheet1 $ _”。 Make sure the object exists and that you spell its name and the path name correctly.
确保对象存在,并且正确拼写了它的名称和路径名。
![]()
My Code to read the excel is- 我阅读Excel的代码是-
private static DataTable getExcelData(string ExcelPath)
{
OleDbConnection con;
string connectionString;
string[] pathArray = ExcelPath.Split('.');
var Extention = pathArray[pathArray.Length - 1];
if (Extention == "xlsx")
//read a 2007 file
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
ExcelPath + ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
else
//read a 97-2003 file
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
ExcelPath + ";Extended Properties=Excel 8.0;";
con = new OleDbConnection(connectionString);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
DataTable dbSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, null);
var firstSheetName = dbSchema.Rows[0]["TABLE_NAME"];
OleDbDataAdapter cmd = new OleDbDataAdapter("select * from [" + firstSheetName + "] Where NOT [Event Code]=''", con);
DataSet ds = new DataSet();
cmd.Fill(ds);
con.Close();
return ds.Tables[0];
}
}
I have to get all the columns inside Mon,Tues etc. 我必须获取星期一,星期二等内的所有列。
GetOleDbSchemaTable also returns hidden tables in your Excel file: usually a name like Sheet1$_
indicates an hidden table created when you apply a filter on Sheet1$
. GetOleDbSchemaTable还可以在Excel文件中返回隐藏表:通常,类似
Sheet1$_
的名称表示在Sheet1$
上应用过滤器时创建的隐藏表。
You need to change your code: search for table that ends with $
to set firstSheetName
. 您需要更改代码:搜索以
$
结尾的表以设置firstSheetName
。
Please note that OLEDB does not preserve the sheet order as they were in Excel . 请注意, OLEDB不会保留Excel中的工作表顺序 。
Also note that you need to do this to read an excel file with multirow titles: 另请注意,您需要执行以下操作以读取具有多行标题的excel文件:
HDR=No
in EXTENDED PROPERTIES
of your connection string EXTENDED PROPERTIES
中设置HDR=No
OleDbCommand
in order to skip the first two rows OleDbCommand
中选择范围,以跳过前两行 For example: 例如:
SELECT [F1] AS Location,
[F2] AS EmpId,
[F3] AS EmpName,
[F4] AS MondayShift,
[F5] AS Monday15Min,
[F6] AS Monday30Min,
[F7] AS Monday15Min2
FROM [Sheet1$A3:G]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.