1
 OleDbConnection excelConnection=null;
            try
            {
                if (Path.GetExtension(excelFileName).Equals(".xls"))
                {
                    string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFileName + ";Extended Properties=" + "\"Excel 8.0 Xml;HDR=YES;IMEX=1;\"";
                    excelConnection = new OleDbConnection(conStr);
                }
                else
                {
                    string conStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelFileName + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\";";
                    excelConnection = new OleDbConnection(conStr);

                }
                excelConnection.Open(); ***// this statement get the error!!!***
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }

这是我的连接字符串:

字符串 1。

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\~Projects IW\Desktop APPS\Maga\Book1.xls;Extended Properties="Excel 8.0 Xml;HDR=YES;IMEX=1;"

字符串 2。

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\~Projects IW\Desktop APPS\Maga\Book1.xlsx;Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1;";

使用连接字符串 1 读取 xls 文件时出现错误

“找不到可安装的 isam”

但是使用连接字符串 2 读取 xlsx 文件;它工作正常:没有错误!

4

2 回答 2

2

错误是由于连接

对于.xls试试这个连接字符串:

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

对于.xlsx试试这个连接字符串:

StrConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + srcFile + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";
于 2015-01-31T10:11:13.037 回答
1

您的第一个连接字符串包括Excel 8.0 Xml;哪些不起作用。改为使用Excel 8.0;

于 2013-04-27T08:08:52.390 回答