I am reading excel sheets (xlsx files) in a asp.net web application with this code
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFileName + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
OleDbConnection excelConnection = new OleDbConnection(connectionString);
excelConnection.Open();
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);
dataAdapter.Dispose();
oleDbCommand.Dispose();
excelConnection.Close();
excelConnection.Dispose();
GC.Collect();
This code works fine on my local machine ( 32 bit Windows 7) and on 64 bit UAT server. I have MS office 2010 installed at both these systems.
The problem is on the client server , a 64 bit Windows Server 2008. My code gives an error as-
System.Data.OleDb.OleDbException (0x80004005): External table is not in the expected format.
at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.OleDb.OleDbConnection.Open()
at ProviderService.ReadDataFromExcel(String excelFileName) in D:\\Admin\MyProject\KDService\ProviderService.cs:line 1374
The code on this line is -
OleDbConnection excelConnection = new OleDbConnection(connectionString);
I checked on this server that MS office is not installed. So I installed the MS Access Database Engine 2010 Redistributable from here
http://www.microsoft.com/en-us/download/details.aspx?id=13255
But still I get the same error mentioned above.
Installing MS office on the client server is my last option. I am looking for a solution where I can fix this using redistributable assemblies.
Please guide me to solve this. Thanks in advance