繁体   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$'..”

我正在尝试使用C#应用程序从Excel工作表中查询数据。 我现在似乎可以连接到文件,但是出现以下异常...

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

我正在使用类似于Excel连接字符串的连接字符串

这是我的代码...

该代码基本上是一种方法(隐藏在类中),该方法使用参数变量基于另一列中提供的数据从一个列返回数据。...该方法适用于我的其他应用程序中的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

我意识到有文献涵盖了使用OLEDB与Excel的连接,但是我认为我将问题归结为表格本身中的已读问题。 同样,这是我第一次尝试连接到Excel。 我打算将工作表视为SQL表,因此我的HDR设置为true。

在扩展属性中将Excel更改为v.14.0

我可能定位的Excel版本错误。 具体来说,excel表是使用Office 2010创建的。使用本文作为参考,我将版本从12.0更改为14.0。

现在我Could not find installable ISAM

好吧,我想我现在已经知道了。 无论如何,至少我能站稳脚跟。

我只需要弄清楚数据类型发生了什么。 我现在Data type mismatch in criteria expression.得到Data type mismatch in criteria expression.

从我的参数变量传递的数据是一个字符串,但我尝试访问的列名(“代码”)在某些行中包含字符串,而在其他行中包含int32。 如果是SQL,我会说此列的类型为CHAR(无论如何)。 在这种情况下,我不知道。

无论如何,这是我自己的解决方案……看来我要做的就是将工作表名称分配给一个变量,然后将该变量包括在我的查询中。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM