简体   繁体   English

尝试查询Excel文件时出现问题…“ Microsoft Office Access数据库引擎找不到对象'Sheet1 $'。”

[英]Issue attempting to query an Excel file… “The Microsoft Office Access database engine could not find the object 'Sheet1$'..”

I am attempting to query data from within an Excel sheet using my C# application. 我正在尝试使用C#应用程序从Excel工作表中查询数据。 I seem to be able to connect to the file now but I am getting the following exception... 我现在似乎可以连接到文件,但是出现以下异常...

The Microsoft Office Access database engine could not find the object 'Sheet1$'.

I'm using a connection string similar to that of Excel connection strings 我正在使用类似于Excel连接字符串的连接字符串

This is my code... 这是我的代码...

The code is basically a method (tucked away in a class) that uses a parameter variable to return a data from one column based on provided data in another....This same method works for SQL connections in my other applications..this is my first swing at an excel sheet however.. 该代码基本上是一种方法(隐藏在类中),该方法使用参数变量基于另一列中提供的数据从一个列返回数据。...该方法适用于我的其他应用程序中的SQL连接。我第一次在Excel工作表中挥杆。

public static string ReturnDefinition(string remCode)
    {
        string strReturnMessage = "";
        string excelConnectString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MEDICARE.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES""";

        OleDbConnection objConn = new OleDbConnection(excelConnectString);
        OleDbCommand objCmd = new OleDbCommand("Select * from [Sheet1$] where Code = @remCode", objConn);
        objCmd.Parameters.AddWithValue("@remCode", remCode);

        try
        {
            objConn.Open();
            OleDbDataReader ExcelDataReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection);
            if (ExcelDataReader.HasRows)
            {
                while (ExcelDataReader.Read())
                {
                    if (string.IsNullOrEmpty((string)ExcelDataReader["Description"]))
                    {
                        strReturnMessage = "** ERROR **";
                    }
                    else
                    {
                        strReturnMessage = ExcelDataReader["Description"].ToString();
                    }
                }
            }
            else
            {
                strReturnMessage = "** ERROR **";
            }
            ExcelDataReader.Close();
            return strReturnMessage;

        }
        catch (Exception ex)
        {
            return "** ERROR **: " + ex.Message;
        }
        finally
        {

        }
    }

5/30 5/30

I realize there is literature out there that covers connections to Excel using OLEDB but I think I've boiled the issue down to a read issue in the sheet its self. 我意识到有文献涵盖了使用OLEDB与Excel的连接,但是我认为我将问题归结为表格本身中的已读问题。 Again, this is the first time I've tried to connect to Excel. 同样,这是我第一次尝试连接到Excel。 My HDR is set to true, as I intend to treat the sheet like a SQL table. 我打算将工作表视为SQL表,因此我的HDR设置为true。

Changed Excel to v.14.0 in Extended Properties 在扩展属性中将Excel更改为v.14.0

I may have been targeting the wrong version of excel. 我可能定位的Excel版本错误。 Specifically, the excel sheet was created using Office 2010. Using This Article for reference, I changed the version from 12.0 to 14.0. 具体来说,excel表是使用Office 2010创建的。使用本文作为参考,我将版本从12.0更改为14.0。

Now I get Could not find installable ISAM 现在我Could not find installable ISAM

Well, I think I've got it figured out for now. 好吧,我想我现在已经知道了。 At least I can get to the sheet anyway. 无论如何,至少我能站稳脚跟。

I'll just need to sort out whats going on with my data types. 我只需要弄清楚数据类型发生了什么。 I now get Data type mismatch in criteria expression. 我现在Data type mismatch in criteria expression.得到Data type mismatch in criteria expression.

The data being passed from my parameter variable is a string but the column name ("Code") I am trying to reach contains strings in some rows and int32 in others. 从我的参数变量传递的数据是一个字符串,但我尝试访问的列名(“代码”)在某些行中包含字符串,而在其他行中包含int32。 If it were SQL I would say this column would be of type CHAR(whatever). 如果是SQL,我会说此列的类型为CHAR(无论如何)。 I don't know in this case. 在这种情况下,我不知道。

Anyway, here is my own resolution...it seems all I needed to do was assign the sheet name to a variable and then include that variable in my query. 无论如何,这是我自己的解决方案……看来我要做的就是将工作表名称分配给一个变量,然后将该变量包括在我的查询中。

enter public static string ReturnDefinition(string remCode)
    {
        string strReturnMessage = "";
        string excelConnectString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MEDICARE.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES""";
        string excelSheet = "Sheet1$";

        OleDbConnection objConn = new OleDbConnection(excelConnectString);
        OleDbCommand excelCmd = new OleDbCommand("Select * from ["+ excelSheet + "] where Code = @remCode", objConn);
        excelCmd.Parameters.AddWithValue("@remCode", remCode);

        try
        {
            objConn.Open();
            OleDbDataReader ExcelDataReader = excelCmd.ExecuteReader(CommandBehavior.CloseConnection);
            if (ExcelDataReader.HasRows)
            {
                while (ExcelDataReader.Read())
                {
                    if (string.IsNullOrEmpty((string)ExcelDataReader["Description"]))
                    {
                        strReturnMessage = "** ERROR **";
                    }
                    else
                    {
                        strReturnMessage = ExcelDataReader["Description"].ToString();
                    }
                }
            }
            else
            {
                strReturnMessage = "** ERROR **";
            }
            ExcelDataReader.Close();
            return strReturnMessage;

        }
        catch (Exception ex)
        {
            return "** ERROR **: " + ex.Message;
        }
        finally
        {

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft Access 数据库引擎找不到对象“Sheet1$” - The Microsoft Access database engine could not find the object 'Sheet1$ Microsoft Jet数据库引擎找不到对象“ Sheet1 $ _” - The Microsoft Jet database engine could not find the object 'Sheet1$_' 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 Office Access数据库引擎找不到对象“ PPR_Status_Detailed” - The Microsoft Office Access database engine could not find the object 'PPR_Status_Detailed' 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 Microsoft Office Access 数据库引擎无法打开或写入文件 - The Microsoft Office Access database engine cannot open or write to the file Microsoft Office Access 数据库引擎无法打开或写入文件 '' - The Microsoft Office Access database engine cannot open or write to the file '' Microsoft jet 数据库引擎在读取 dbf 文件时找不到对象 - The Microsoft jet database engine could not find object while reading dbf file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM