Microsoft.Jet.OLEDB.4.0 导入excel报"找不到可安装的ISAM&qu

时间:2021-08-26 02:09:59

用使用Microsoft.Jet.OLEDB.4.0导入Excel文件时,报“找不到可安装的ISAM"错误原因:

string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;"   "Data Source="   fileName   ";Extended Properties=Excel 8.0;;HDR=YES;IMEX=1";
            using (OleDbConnection conn = new OleDbConnection(connstring))
            {
                conn.Open();
                DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new Object[] { null, null, null, "TABLE" });//得到所有sheet的名字  
                string firstSheetName = sheetsName.Rows[0][2].ToString(); //得到第一个sheet的名字  
                string sql = string.Format("SELECT * FROM [{0}]", firstSheetName); //查询字符串  
                //string sql = string.Format("SELECT * FROM [{0}] WHERE [日期] is not null", firstSheetName); //查询字符串  
                OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
                DataSet set = new DataSet();                
                ada.Fill(set);
                return set.Tables[0];
            }
             

将上述代码中

connstring = "Provider=Microsoft.Jet.OLEDB.4.0;"   "Data Source="   fileName   ";Extended Properties=Excel 8.0;;HDR=YES;IMEX=1
加上单引号,如下,即可解决问题。
connstring = "Provider=Microsoft.Jet.OLEDB.4.0;"   "Data Source="   fileName   ";Extended Properties=‘Excel 8.0;;HDR=YES;IMEX=1‘";