Domanda

I'm working on a project that would be able to convert Excel files to .CSV File, I think there are some problems in my C# code that is generating and error message Could Not Find Installable ISAM, please help me to sort out my problem.

Code:

if (dlgOne.FileName.EndsWith(".xlsx"))
{
    StrConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + srcFile + ";Extended Properties=\"Excel 12.0;\"";
}

if (dlgTwo.FileName.EndsWith(".xls"))
{
    StrConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + srcFile + ";Extended Properties=\"Excel 1.0;HDR=Yes;IMEX=1\"";
}

OleDbConnection conn = null;

conn = new OleDbConnection(StrConn);
conn.Open();  <------------ throw exception

In debug mode the application throws an exception (line: conn.Open();) I searched the Internet and I found that I have to put the Data Source between one cotes but it doesn't work in my case.

È stato utile?

Soluzione

Both of the connection strings are wrong.

For .xlsx, it should be:

StrConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + srcFile + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";

(Note the extra Xml part, the HDR=YES to indicate your file has headers, the IMEX=1 to treat all data as text and the repositioned semi-colon. You would need different connection strings for .xlsm and .xlsb files - see here)

For .xls, it should be:

StrConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + srcFile + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";

(Note the change from Excel 1.0 to Excel 8.0 and the addition of a semi-colon at the end)

Altri suggerimenti

Platform plays an important role: if your code compiles in 64-bit and you have Office 32-bit installed (which means all ODBC, ISAM, etc. drivers are 32-bit). Try compiling with "Any CPU" platform

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top