您的位置:首页 - 教程 - .NET - 正文
.NET读取Excel

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;

评论: