ADO.NET读取Excel文件并作为数据源2015-12-31 cnblogs 愤怒的TryCatch项目中需要用的功能,贴上代码了。需要注意的地方:配置Web.config的时候要注意版本问题!
//若是在Web.config中配置数据源,如下<add key="ExcelConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source="miroExcel/Info.xls";Extended Properties="Excel 8.0;HDR=yes;IMEX=2""/> /// <summary>/// 读取Excel文件传入绝对路径查询条件/// </summary>/// <returns>DataSet</returns>public DataSet GetBoolExcelDAL(string cardId, string excelPath){try{string strCon = "Provider=Microsoft.Ace.OleDb.12.0; Data Source ="" + excelPath + "";Extended Properties="Excel 12.0;HDR=yes;IMEX=2"";OleDbConnection Conn = new OleDbConnection(strCon);string strsql = string.Format("select * from [Sheet1$] where CardId="{0}"", cardId);Conn.Open();OleDbCommand oleCommand = new OleDbCommand(strsql, Conn);OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);DataSet ds = new DataSet();oleAdapter.Fill(ds, "ExcelData");if (ds != null){return ds;}else{return null;}}catch (Exception ex){return null;}