.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 [日志数] from [Sheet1$] where [用户角色] <> '教研组长' group by [所在组]", cnnxls);
DataSet ds = new DataSet();
//将Excel里面有表内容装载到内存表中!
DataTable tbContainer = new DataTable();
oda.Fill(tbContainer);
return tbContainer;

 

posted @ 2016-10-27 10:22    阅读(429)  评论(0编辑  收藏  举报