5

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.

Factor Mystic
  • 26,279
  • 16
  • 79
  • 95
user1863359
  • 317
  • 1
  • 10
  • 23

2 Answers2

19

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)

barrowc
  • 10,444
  • 1
  • 40
  • 53
  • Thank You @barrowc , you are one of those who make the life better – user1863359 Jul 30 '13 at 09:05
  • (Note the change from Excel 1.0 to Excel 8.0 and the addition of a semi-colon at the end), I did not understand what that mean ... but I have noticed that in my generated file there an additional column how can I remove it – user1863359 Jul 30 '13 at 10:12
  • I have noticed that in my generated file there an additional column how can I remove it – user1863359 Jul 30 '13 at 12:38
  • You would need to show the code which actually produces the CSV file to see why the additional column appears. That should probably be a new question – barrowc Jul 31 '13 at 05:05
  • you already mentioned in your last response that there will be an additional column (Note the change from Excel 1.0 to Excel 8.0 and the addition of a semi-colon at the end) – user1863359 Jul 31 '13 at 10:20
  • No - that was a description of what had actually changed in the connection string (you had "Excel 1.0" instead of "Excel 8.0" and there was a semi-colon missing) and should have no effect on the structure of the resulting CSV file – barrowc Jul 31 '13 at 15:09
  • Thanks a ton it really helped me with my issue – vini Jan 09 '14 at 14:20
1

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

PricklyMaster
  • 85
  • 1
  • 7