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.

Community
  • 1
  • 1
Lahib
  • 1,305
  • 5
  • 34
  • 62
  • Are you giving a fully qualified filename ? – prthrokz Jan 08 '13 at 10:15
  • are youdefinate the existing excel sheet you are trying to open is the correct version youve coded into the string? – RhysW Jan 08 '13 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. – Tim Schmelter Jan 08 '13 at 10:16
  • @Lahib: What version of excel are you trying to open, xls or xlsx? – Tim Schmelter Jan 08 '13 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"** – Lahib Jan 08 '13 at 10:20
  • Is the file still open in `Excel` when you try to open the connection programmatically? If so, close it and then, try again. – Alex Filipovici Jan 08 '13 at 10:21
  • The file os not opened. But i do generate 2 files, one serverside log and one client download. dunno if it has something to say – Lahib Jan 08 '13 at 10:43
  • @TimSchmelter its xlsx files – Lahib Jan 08 '13 at 10:48

1 Answers1

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.

Lahib
  • 1,305
  • 5
  • 34
  • 62
  • Hi does it mean that no excel created with OpenXML can be read by OleDB? – Sebastian Widz Nov 12 '19 at 10:33
  • @SebastianWidz at the time i wrote this i was not able to do so. – Lahib Nov 13 '19 at 08:53
  • 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 – Sebastian Widz Nov 13 '19 at 16:16