簡體   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