asp.net 导出excel--NPOI
1.使用OLEDB导出Excel ,这种方式有点慢,慎用
/// <summary>
/// 使用OLEDB导出Excel
/// </summary>
/// <param name="dt"></param>
/// <param name="filepath"></param>
/// <param name="tablename"></param> public static void Export(System.Data.DataTable dt, string filepath, string tablename)
{ //excel 2003格式 // string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;"; //Excel 2007格式 string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=Excel 12.0 Xml;"; try
{ using (OleDbConnection con = new OleDbConnection(connString))
{
con.Open();
StringBuilder strSQL = new StringBuilder();
strSQL.Append("CREATE TABLE ").Append("[" + tablename + "]"); strSQL.Append("(");
for (int i = ; i < dt.Columns.Count; i++)
{
strSQL.Append("[" + dt.Columns[i].ColumnName + "] text,"); }
strSQL = strSQL.Remove(strSQL.Length - , );
strSQL.Append(")"); OleDbCommand cmd = new OleDbCommand(strSQL.ToString(), con);
cmd.ExecuteNonQuery();
for (int i = ; i < dt.Rows.Count; i++)
{
strSQL.Clear(); StringBuilder strfield = new StringBuilder(); StringBuilder strvalue = new StringBuilder(); for (int j = ; j < dt.Columns.Count; j++)
{ strfield.Append("[" + dt.Columns[j].ColumnName + "]"); strvalue.Append("'" + dt.Rows[i][j].ToString() + "'"); if (j != dt.Columns.Count - )
{ strfield.Append(","); strvalue.Append(","); } else
{ } } cmd.CommandText = strSQL.Append(" insert into [" + tablename + "]( ") .Append(strfield.ToString()) .Append(") values (").Append(strvalue).Append(")").ToString(); cmd.ExecuteNonQuery(); } con.Close(); } Console.WriteLine("OK"); } catch (Exception ex)
{ Console.WriteLine(ex.Message); } }
2.NPOI 导出excel
2007 版
public FileResult Export(SC_ServiceCardUsedRecordSearchParam param)
{ int cou = ; //创建Excel文件的对象
//NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();// NPOI.XSSF.UserModel.XSSFWorkbook book = new NPOI.XSSF.UserModel.XSSFWorkbook();
//NPOI.XSSF.UserModel.XSSFWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook();
//添加一个sheet
//NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1"); // NPOI.SS.UserModel.ISheet sheet1= book.CreateSheet("Sheet1"); // List<SC_ServiceCardUsedRecord> cards = new List<SC_ServiceCardUsedRecord>(); //获取list数据
cards = _AppContext.ServiceCardUsedRecordApp.SelectRecordList(param).ToList<SC_ServiceCardUsedRecord>(); //给sheet1添加第一行的头部标题
NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow();
row1.CreateCell().SetCellValue("编号");
row1.CreateCell().SetCellValue("卡券号码");
row1.CreateCell().SetCellValue("手机号");
row1.CreateCell().SetCellValue("姓名");
row1.CreateCell().SetCellValue("车架号");
row1.CreateCell().SetCellValue("经销商");
row1.CreateCell().SetCellValue("行驶里程");
row1.CreateCell().SetCellValue("创建时间");
row1.CreateCell().SetCellValue("卡劵类型");
row1.CreateCell().SetCellValue("车型");
row1.CreateCell().SetCellValue("来源");
row1.CreateCell().SetCellValue("经销商名称");
row1.CreateCell().SetCellValue("发卡时间");
row1.CreateCell().SetCellValue("购车时间");
row1.CreateCell().SetCellValue("会员等级"); //将数据逐步写入sheet1各个行
for (int i = ; i < cards.Count(); i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + ); rowtemp.CreateCell().SetCellValue(cards[i].Id.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].CardNo.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].PhoneNumber.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].CustName == null ? "" : cards[i].CustName.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].VIN == null ? "" : cards[i].VIN.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].DealerId == null ? "" : cards[i].DealerId.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].Mileage == null ? "" : cards[i].Mileage.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].CreateTime.ToLongDateString());
rowtemp.CreateCell().SetCellValue(cards[i].CardTypeName == null ? "" : cards[i].CardTypeName.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].CarCategory == null ? "" : cards[i].CarCategory.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].ActivityTag == null ? "" : cards[i].ActivityTag.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].DealerName == null ? "" : cards[i].DealerName.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].kqCreateTime == null ? "" : cards[i].kqCreateTime.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].BuyTime == null ? "" : cards[i].BuyTime.ToString());
rowtemp.CreateCell().SetCellValue(cards[i].MLevel == null ? "" : cards[i].MLevel.ToString()); } // 写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
// MemoryStream stream = new MemoryStream();
book.Write(ms);
var buf = ms.ToArray();
//ms.Seek(0, SeekOrigin.Begin); //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
//return File(ms, "application/vnd.ms-excel", "Card.xlsx");
return File(buf, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Card.xlsx");
}
2003 版
public ActionResult Export(DateTime? startTime, DateTime? endTime, string tableName)
{
IWorkbook book = new HSSFWorkbook(); //获取list数据
DataTable dt = _AppContext.ReportApp.GetReport(startTime ?? DateTime.Now.AddDays(-), endTime ?? DateTime.Now, tableName); if (dt.Rows.Count < EXCEL03_MaxRow)
DataWrite2Sheet(dt, , dt.Rows.Count - , book, "Sheet1");
else
{
int page = dt.Rows.Count / EXCEL03_MaxRow;
for (int i = ; i < page; i++)
{
int start = i * EXCEL03_MaxRow;
int end = (i * EXCEL03_MaxRow) + EXCEL03_MaxRow - ;
DataWrite2Sheet(dt, start, end, book, "Sheet" + i.ToString());
}
int lastPageItemCount = dt.Rows.Count % EXCEL03_MaxRow;
DataWrite2Sheet(dt, dt.Rows.Count - lastPageItemCount, dt.Rows.Count - , book, "Sheet" + page.ToString());
} // 写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
book.Write(ms);
ms.Seek(, SeekOrigin.Begin);
return File(ms, "application/vnd.ms-excel", tableName + ".xls");
}
注意:操作Excel2003与操作Excel2007使用的是不同的命名空间下的内容
使用NPOI.HSSF.UserModel空间下的HSSFWorkbook操作Excel2003
使用NPOI.XSSF.UserModel空间下的XSSFWorkbook操作Excel2007
asp.net 导出excel--NPOI的更多相关文章
- Asp.net导出Excel续章(自定义合并单元格,非Office组件)
结合上次写的导出Excel方法,这次上头要求我将列头进行一下合并 以前的效果: 改进后的效果: 在上篇文章中写到了Excel的导出方法,这次为了避免在生产环境中使用Office组件,服务器各种权限配置 ...
- asp.net导出excel示例代码
asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> ); ; ...
- [转] Asp.Net 导出 Excel 数据的9种方案
湛刚 de BLOG 原文地址 Asp.Net 导出 Excel 数据的9种方案 简介 Excel 的强大之处在于它不仅仅只能打开Excel格式的文档,它还能打开CSV格式.Tab格式.website ...
- ASP.NET导出EXCEL类
最新ASP.NET导出EXCEL类 说明:可以导出ASP.NET页面和DATAGRID(WebControl)数据,可以导出表单头 using System;using System.Data;usi ...
- asp.net导出excel并弹出保存提示框
asp.net导出excel并弹出保存提示框 2013-07-12 | 阅:1 转:78 | 分享 腾讯空间 人人网 开心网 新浪微博 腾讯微博 搜狐空间 推荐给朋友 举报 ...
- ASP.NET导出Excel(利用NPOI和EPPlus库,无需安装Office)
网上提供了很多Asp.net中操作Excel的方法,其中大部分是调用微软的Office组件,下面提供三个无须安装Office即可从Asp.net输出Excel的方法. 1 简单方法 //下面代码输出的 ...
- ASP.NET MVC导出excel npoi
使用npoi组件 前端代码: @Html.ActionLink("导出Excel", "ExportWarehouseInOutDetailTable", ne ...
- asp.net 导出Excel
分享一个asp.net 导出假Excel代码.优点,不用借助于任何插件比如(NPOI),复制代码,修改grid.DataSource直接导出. 先看导出后的效果图 System.Web.UI.WebC ...
- ASP.NETCore -----导出Excel文件并下载
本事例分为nopi(安装DotNetCore.NPOI)下载和EPPlus(EPPlus.Core.dll)下载,其中npoi下载演示的是根据执行的模板进行数据下载 npoi帮助类NpoiExcelU ...
- Asp.net导出Excel乱码的解决方法
通过跟踪Asp.net服务器代码,没有乱码,然而导出Excel到浏览器后,打开时出现乱码. 解决方法是添加编码格式的前缀字节码:Response.BinaryWrite(System.Text.Enc ...
随机推荐
- python 文档
python 文档 https://docs.python.org/2/library/index.html
- Mysql Window 解压版卸载
windows如何彻底卸载mysql 如何彻底删除mysql 1.首先在windows服务中将mysql服务删掉,使用命令 sc delete mysql 2.在控制面板中卸载掉mysql. 3.清理 ...
- vscode 同步配置
按照插件完成配置之后,将token保存在本地,编辑配置文件:ctrl+3.sync高级选项.编辑本地扩展设置.将token粘贴进去即可,团队所有vscode均应该按照此配置来保持统一:
- duilib中字体font设置
<Font name="微软雅黑" size="9" bold="false"/> <Label name="n ...
- asp.net session锁导致ajax请求阻塞
问:为了可以顺序访问Session的状态值,Session是否提供了锁定机制?答:Session实现了Reader/Writer的锁机制:当页面对Session具有可写功能(即页面有<%@Pag ...
- Gym 101873G - Water Testing - [皮克定理]
题目链接:http://codeforces.com/gym/101873/problem/G 题意: 在点阵上,给出 $N$ 个点的坐标(全部都是在格点上),将它们按顺序连接可以构成一个多边形,求该 ...
- [qemu] 差分盘使用
我要装好多台同样配置的虚拟机.比如10台CentOS7, 各自有不同的配置. 这样的话装10台kvm虚拟机,特别的浪费空间,image文件相互迁移的话也不方便. 这个时候可以选择差分盘:就是先装一个标 ...
- apache tomcat (catalina)查版本(solaris/unix)
先进到tomcat的bin目录下(cd /tomcat目录/bin),在执行./version.sh https://blog.csdn.net/vv___/article/details/78653 ...
- Java+selenium如何清理浏览器Cookie
一.场景:在未注销系统切换不同账号登录系统下,登录会有浏览器缓存,导致登录时间异常缓慢.跟开发浏览器缓存处理机制有关系. 二.解决方法: 获取浏览器Cookie,在Login方法前增加清除缓存的代码. ...
- TZOJ 3134: 渊子赛马修改版
描述 赛马是一古老的游戏,早在公元前四世纪的中国,处在诸侯割据的状态,历史上称为“战国时期”.在魏国作官的孙膑,因为受到同僚庞涓的迫害,被齐国使臣救出后,到达齐国国都. 赛马是当时最受齐国贵族欢迎的娱 ...