Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I am having the code as follows in vb.net

VB
Dim connectionString As String =[String].Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", filepath)
            Using oleDbConn As New OleDbConnection(connectionString)
                oleDbConn.Open()



when i try to open *.xls file it works fine but if I try to open *.xlsx page than it throws me an error External table is not in the expected format. I searched in net but could not sort out with it. Please someone help me to sort it out its really urgent
Posted
Updated 29-Jun-20 6:49am
v2

Use the following code as the Excel 2007 vcersion files are Excel 12.0

Dim connectionString As String =[String].Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1;""", filepath)


Best of luck!!
 
Share this answer
 
Comments
sagarjainhr 31-Jan-13 4:56am    
It didnt work
You need OLEDB 12 drivers installed on your machine.
Install from this link

http://www.microsoft.com/en-us/download/details.aspx?id=13255[^]
And the code will be like this .

C#
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFileName + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
            // if you don't want to show the header row (first row) use 'HDR=NO' in the string
            OleDbConnection excelConnection = new OleDbConnection(connectionString);
            excelConnection.Open();
            // Get the data table containg the schema guid.
            DataTable dtWorksheetTables = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            if (dtWorksheetTables == null || dtWorksheetTables.Rows.Count == 0) return null;
            string worksheetName = GetWorksheetName(dtWorksheetTables);
            string strExcelSQL = "SELECT * FROM [" + worksheetName + "]";
            OleDbCommand oleDbCommand = new OleDbCommand(strExcelSQL, excelConnection);
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(oleDbCommand);
            DataTable excelDataTable = new DataTable();
            dataAdapter.Fill(excelDataTable);
            //Dispose 
            dataAdapter.Dispose();
            oleDbCommand.Dispose();
            excelConnection.Close();
            excelConnection.Dispose();
            GC.Collect();
 
Share this answer
 
v3
Comments
sagarjainhr 31-Jan-13 4:56am    
I have installed access database 10 installed in my machine still its throwing an error
bbirajdar 31-Jan-13 5:03am    
Change the conenction string to

@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFileName + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
sagarjainhr 31-Jan-13 5:06am    
I am using the vb.net platform and not C# so cant use "\" slashes as its throws an error.
If I convert it to vb.net and use also its throwing the same error
bbirajdar 31-Jan-13 5:11am    
Dont look at the \ . Look at the contents of the connection string.... You are using Excel 8.0.. Instead use 12.0.. Did you get my point or should I explain it in more detail?
sagarjainhr 31-Jan-13 5:19am    
I tried using Excel 12.0 still its giving the same error. I am struck up over here and I am really not getting what to do I tried with 8 & 12 both but still not able I am getting the same error
I solved it by my self there was no problem with the connection string and everything was fine over there. The problem was with I was checking whether the file is open or not and I have not closed that file after that process. so it was used by other process and connection string was failing to access the excel sheet and it was throwing an error.

Please ensure that the file is not open or used by any other process while trying to read with *.xlsx file, If file is open also it works fine with *.xls and not in *.xlsx file keep it in mind while accessing *.xlsx file
 
Share this answer
 
v2
Comments
vikramaditya9 26-Nov-13 3:20am    
I have also facing same problem "External table is not in the expected format".I am using Connectionstring like > ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelPath + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
Please suggest anyone any problem with connectionstring?
Anshuwa 23-Dec-14 6:14am    
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Files\file.xlsx; Extended Properties='Excel 8.0;HDR=Yes
WEI LI JASMINE ZEE 30-May-18 23:40pm    
YES. The selected excel file must be opened and it will have no problem with the connection string.
If the selected excel file is closed, I will get an error of 'External table is not in the expected format'.
Is there any way to solve it? TQ.
Change connection string:
http://www.connectionstrings.com/excel-2007[^]
 
Share this answer
 
Comments
sagarjainhr 31-Jan-13 4:57am    
I have tried with this link still not able to sort it out
You may have more than one excel/database sheet open in the background.... so make sure you have the right sheet open/closed. I closed all sheet the reconnected to database.
 
Share this answer
 
Comments
Dave Kreskowiak 29-Jun-20 13:13pm    
Asked and answered seven years ago. Next time, read all of the answers before answering yourself.
I tried all suggested here but none worked..and finally i found out that a particular cell was having more than 12k characters. so look out for length as well.
 
Share this answer
 
i was also facing an same issue in which while reading data from an excel i was getting external table error.

basically the excel file i was trying to read was code generated. code generated excel files do not have a [Program Name] property under Details tab. Due to which the driver mis behaves.

if u open and save as the file the issue will get resolved or else you can save the file before reading using the below code will solve ur issue

// Initialize Excel Application
Excel.Application excelApp = new Excel.Application();
// Open Existing Excel
Excel.Workbook excelWorkBook = excelApp.Workbooks.Open("/Ur path..");


// Save Excel
excelWorkBook.Save();
// Close Excel
excelWorkBook.Close();
// Close Excel Application
excelApp.Quit();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900