ASP.Net读取Excel,无法找到可安装的ISAM
代码如下,以前均可以执行,现在我试了N种办法,不存在引号问题,msexcl40注册成功,但是还是报找不到可安装的ISAM
我的Excel版本是2003、2007,.Net是2008
报错的位置是da.fill() (da是OleDbDataAdapter),如果OleDbConnection对象Con.open()时也会报找不到可安装的ISAM
string strcon = string.Empty;
string strFilePath=UpKaoqin.PostedFile.FileName;
string strExt = strFilePath.Substring(strFilePath.LastIndexOf(".") + 1).ToLower();
if (strExt == "xls")
{
strcon = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=" + strFilePath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
//strcon = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=" + strFilePath + ";Extended Properties='Excel 8.0'";
//strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;DataSource=" + strFilePath;
}
if (strExt == "xlsx")
{
//strcon = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=" + strFilePath + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1';Persist Security Info=True";
strcon = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=" + strFilePath + ";Extended Properties='Excel 12.0;HDR=NO;IMEX=1';Persist Security Info=True";
}
System.Data.OleDb.OleDbConnection Con = new System.Data.OleDb.OleDbConnection(strcon);
//Application.doevents();
try
{
//Con.Open();
string strCom = "Select * From [Sheet1$]";
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand(strCom, Con);
OleDbDataAdapter da = new OleDbDataAdapter(myCommand);
DataSet ds = new DataSet();
//myCommand.Fill(ds,"[Sheet1$B1:C2]");
da.Fill(ds);
DataTable dt = ds.Tables[0];
// Con.Close();
}
catch
{
}