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.

4

2 に答える 2

18

両方の接続文字列が間違っています。

.xlsx の場合、次のようになります。

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

(追加の Xml 部分、ファイルにヘッダーがあることを示す HDR=YES、すべてのデータをテキストとして扱う IMEX=1、および再配置されたセミコロンに注意してください。.xlsm ファイルと .xlsb ファイルには異なる接続文字列が必要です。ここで

.xls の場合は、次のようになります。

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

(Excel 1.0 から Excel 8.0 への変更と、末尾にセミコロンが追加されていることに注意してください)

于 2013-07-25T22:05:35.207 に答える
1

プラットフォームは重要な役割を果たします。コードが 64 ビットでコンパイルされ、Office 32 ビットがインストールされている場合 (つまり、ODBC、ISAM などのドライバーはすべて 32 ビットです)。「Any CPU」プラットフォームでコンパイルしてみる

于 2013-07-25T15:08:15.127 に答える