【问题标题】:Issue attempting to query an Excel file... "The Microsoft Office Access database engine could not find the object 'Sheet1$'.."尝试查询 Excel 文件时出现问题...“Microsoft Office Access 数据库引擎找不到对象 'Sheet1$'..”
【发布时间】:2014-07-19 13:58:23
【问题描述】:

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

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

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

这是我的代码...

该代码基本上是一种方法(隐藏在一个类中),它使用参数变量根据另一列中提供的数据从一列返回数据...。同样的方法适用于我的其他应用程序中的 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。我的 HDR 设置为 true,因为我打算将工作表视为 SQL 表。

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

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

现在我得到Could not find installable ISAM

【问题讨论】:

  • “Sheet1$”中的美元符号相当奇怪。您确定这是工作簿中电子表格的实际名称吗?
  • 我想没关系。我试过有无,两种尝试都会产生相同的错误。我读过的大多数文章都包括 Sheet1 之后的美元,所以我认为这是必要的。

标签: c# excel oledb


【解决方案1】:

嗯,我想我现在已经弄清楚了。 不管怎样,至少我可以找到工作表。

我只需要弄清楚我的数据类型发生了什么。 我现在得到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
        {

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多