程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> .NET讀取Excel

.NET讀取Excel

編輯:關於.NET

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;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved