简体   繁体   English

Microsoft Jet数据库引擎找不到对象“ Sheet1 $ _”

[英]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文件:

  • set HDR=No in EXTENDED PROPERTIES of your connection string 在连接字符串的EXTENDED PROPERTIES中设置HDR=No
  • specify column name and select range in your 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.

相关问题 GetColumnName()LinqToExcel for csv文件抛出错误Microsoft Jet数据库引擎找不到对象'Sheet1 $ .txt' - GetColumnName() LinqToExcel for csv file throw error The Microsoft Jet database engine could not find the object 'Sheet1$.txt' Microsoft Access 数据库引擎找不到对象“Sheet1$” - The Microsoft Access database engine could not find the object 'Sheet1$ Microsoft Jet数据库引擎找不到对象“ ...” - The Microsoft Jet database engine could not find the object '…' Microsoft Jet数据库引擎找不到对象 - The Microsoft Jet database engine could not find the object Microsoft Jet数据库引擎找不到对象 - The Microsoft Jet database engine could not find the object 尝试查询Excel文件时出现问题…“ Microsoft Office Access数据库引擎找不到对象'Sheet1 $'。” - Issue attempting to query an Excel file… “The Microsoft Office Access database engine could not find the object 'Sheet1$'..” Microsoft jet 数据库引擎在读取 dbf 文件时找不到对象 - The Microsoft jet database engine could not find object while reading dbf file Microsoft Jet数据库引擎找不到输入表或查询 - The Microsoft Jet database engine cannot find the input table or query Microsoft Office Access数据库引擎找不到对象“ PPR_Status_Detailed” - The Microsoft Office Access database engine could not find the object 'PPR_Status_Detailed' 使用Microsoft Jet引擎读取XLS文件 - Reading an XLS file with the Microsoft Jet Engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM