項目中需要用的功能,貼上代碼了。
需要注意的地方:配置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; }
查看本欄目