1、代碼
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties=\"Excel 12.0;HDR=Yes;\""; //string strConn = "Provider=Microsoft.ACE.OLEDB.14.0;Data Source=" + Path + ";Extended Properties=\"Excel 14.0;HDR=Yes;\""; // Office 07及以上版本 不能出現多余的空格 而且分號注意 //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"; //string strConn = "Provider=Microsoft.ACE.OLEDB.16.0;Data Source=" + Path + ";Extended Properties=\"Excel 16.0;HDR=Yes;IMEX=1;\""; // Office 07及以上版本 不能出現多余的空格 而且分號注意 OleDbConnection conn = new OleDbConnection(strConn); OleDbConnection cnnxls = new OleDbConnection(strConn); //讀取Excel裡面有 表Sheet1 OleDbDataAdapter oda = new OleDbDataAdapter(string.Format("select * from [{0}$]", "Sheet1"), cnnxls); //OleDbDataAdapter oda = new OleDbDataAdapter("select [所在組] as [班級名稱],count([所在組]) as [學員數],sum(case [登陸次數] when [登陸次數] > 0 then 1 else 0 end) as [登陸率],0 as [合格率],sum([日志]) as [日志數],sum([生成性資源]) as [生成性資源數],sum([互點互評]) as [互點互評數],sum([推薦日志]) as [推薦日志數],sum([推薦資源]) as [推薦資源數],sum([公告簡報]) as [公告簡報數],sum([坊間手拉手]) as [坊間手拉手],sum([微課]) as [微課數] from [Sheet1$] where [用戶角色] <> '教研組長' group by [所在組]", cnnxls); DataSet ds = new DataSet(); //將Excel裡面有表內容裝載到內存表中! DataTable tbContainer = new DataTable(); oda.Fill(tbContainer); return tbContainer;