问题描述:今天处理Excel时遇到一个问题,本地使用Microsoft.Jet.OLEDB.4.0处理,正常完成了需求,
上传到服务器后发生了异常,通过排查发现问题出现在对Excel文件的读取上,然后推测是因为
没有安装对应的驱动,,然后在微软官网查到了Microsoft.ACE.OLEDB.12.0;这个驱动,
服务器上安装后变成功解决了此问题,同时需要注意,连接字符串需要改成Microsoft.ACE.OLEDB.12.0;
示例
public static DataSet ExcelToDS(string Path) { //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;"; //发布后开启此注释 string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; DataSet ds = null; strExcel = "select * from [sheet1$]"; myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet(); myCommand.Fill(ds, "table1"); return ds; }