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.