导入Excel表时,提示"找不到可安装的ISAM"

C#读取Excel代码:

string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";//不能是11.0
            OleDbConnection conn = new OleDbConnection(strConn);
            conn.Open();
            string strExcel = "select * from [sheet1$]";
            OleDbDataAdapter da = new OleDbDataAdapter(strExcel, strConn);
            DataSet ds = new DataSet();
            try
            {
                da.Fill(ds);
            }
            catch (Exception ex)
            {
                throw new Exception("读取Excel失败:"+ex.Message);
            }
            return ds;
如果提示"找不到可安装的ISAM",通过google,有两个解决办法:

1.连接字符串问题

Extended Properties='Excel 8.0;HDR=NO;IMEX=1'

//HDR表示要把第一行作为数据还是作为列名,作为数据用HDR=no,作为列名用HDR=yes;通过Imex=1来把混合型作为文本型读取,避免 null值。

上句的左右两个单引号不能少

 

2.只需注册 Excel ISAM即可

  在“运行”对话框中输入回车即可:Regsvr32 c:\WINDOWS\system32\msexcl40.dll

posted @ 2011-03-14 22:56  Nuoya  阅读(11384)  评论(1编辑  收藏  举报