/// <summary> /// 根据EXCEL路径生成OleDbConnectin对象 /// </summary> /// <param name="ExcelFilePath">EXCEL文件相对于站点根目录的路径</param> /// <param name="Verion">Excel数据驱动版本:97-2003或2007,分别需要安装数据驱动软件</param> /// <returns>OleDbConnection对象</returns> public static OleDbConnection CreateConnection(string ExcelFilePath, ExcelVerion Verion) { OleDbConnection Connection = null; string strConnection = string.Empty; try { switch (Verion) { case ExcelVerion.Excel2003: //读取Excel97-2003版本 strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + HttpContext.Current.Server.MapPath(ExcelFilePath) + ";Extended Properties=Excel 8.0"; break; case ExcelVerion.Excel2007: //读取Excel2007版本 strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';data source=" + ExcelFilePath; break; } if (!string.IsNullOrEmpty(strConnection)) { Connection = new OleDbConnection(strConnection); } } catch (Exception) { } return(Connection); }
/// <summary> /// 根据EXCEL路径生成OleDbConnectin对象 /// </summary> /// <param name="ExcelFilePath">EXCEL文件相对于站点根目录的路径</param> /// <param name="Verion">Excel数据驱动版本:97-2003或2007,分别需要安装数据驱动软件</param> /// <returns>OleDbConnection对象</returns> public static OleDbConnection CreateConnection(string ExcelFilePath, ExcelVerion Verion) { OleDbConnection Connection = null; string strConnection = string.Empty; try { switch (Verion) { // case ExcelVerion.Excel2003: //读取Excel97-2003版本 // strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " + //"Data Source=" + AppDomain.CurrentDomain.BaseDirectory + ";Extended Properties=Excel 8.0"; // break; case ExcelVerion.Excel2007: //读取Excel2007版本 strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';data source=" + ExcelFilePath; break; } if (!string.IsNullOrEmpty(strConnection)) Connection = new OleDbConnection(strConnection); } catch (Exception) { } return Connection; }