简体   繁体   中英

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. 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

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..

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

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. Again, this is the first time I've tried to connect to Excel. My HDR is set to true, as I intend to treat the sheet like a SQL table.

Changed Excel to v.14.0 in Extended Properties

I may have been targeting the wrong version of 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.

Now I get 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.

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. If it were SQL I would say this column would be of type CHAR(whatever). 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
        {

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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