3

Possible Duplicate:
Excel “External table is not in the expected format.”

What I'm doing is getting all the table names and inserting them into a list. This is my code:

public List<string> GetEditExcelSheets(string fileName, out OleDbException Error)
{
    List<string> Result = new List<string>();
    Error = null;
    if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
    {
        string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0 XML;HDR=YES;IMEX=1\"";

        if (!string.IsNullOrEmpty(connectionString))
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                try
                {
                    connection.Open();
                    DataTable tables = connection.GetSchema("Tables");
                    foreach (DataRow row in tables.Rows)
                    {
                        string TableName = Convert.ToString(row["TABLE_NAME"]);
                        Result.Add(TableName);
                    }
                }

                catch (OleDbException ex)
                {
                    Error = ex;
                }
                finally
                {
                    if (connection.State == System.Data.ConnectionState.Open)
                    {
                        connection.Close();
                    }
                }
            }
        }
    }
    return Result;
}

I'm getting this error:

"'External table is not in the expected format'"

when reaching this code line:

connection.Open();

I have tried editing the connection string a couple of times after searching for solutions on google. But no other connection string helps me, and this connection string should work. I can seem to figure out what I'm doing wrong.

8
  • Are you giving a fully qualified filename ? Commented Jan 8, 2013 at 10:15
  • are youdefinate the existing excel sheet you are trying to open is the correct version youve coded into the string? Commented Jan 8, 2013 at 10:15
  • @James (and others): have you read the proposed duplicate? At least the accepted answer does not help since he uses already that connection-string. Commented Jan 8, 2013 at 10:16
  • @Lahib: What version of excel are you trying to open, xls or xlsx? Commented Jan 8, 2013 at 10:20
  • I have just checked that the file it is using contains a sheet, and it is using the right excel file. and there os not duplicate of this file. this is what the full connection string looks like : Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\users\lmy\documents\visual studio 2012\Projects\CustomerImportV2\CustomerImportV2\Upload\20130108111646excel.xlsx;Extended Properties="Excel 12.0 XML;HDR=YES;IMEX=1" Commented Jan 8, 2013 at 10:20

1 Answer 1

1

I found the solution for this. As i am using open XML sdk the xlsx file is saved with the xlsx extension but the file is not an excel file. It's an open xml file that is saved with xlsx extension so Excel can open it. That means i cant use sql query to read the data in the file.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi does it mean that no excel created with OpenXML can be read by OleDB?
@SebastianWidz at the time i wrote this i was not able to do so.
I am asking because I am struggling with exactly the same issue. I wonder if this is possible, I know that EPPlus is able to do that, but they write pure XML. With pure OpenXML i get the described error, and I am not sure which part of xls is not liked by OleDb

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.