ASP.NET MVC导出excel npoi
使用npoi组件
前端代码:
@Html.ActionLink("导出Excel", "ExportWarehouseInOutDetailTable", new {warehouseInOutId = Model.Id},new {@class = "btn btn-primary pull-right"})
@*ie8低版本以下不支持h5的formaction特性,故改用下一种方法。但这样有个小bug:修改查询条件后点击“导出Excel”只会导出条件未修改前的数据,但“查询”按钮无此问题*@
@*<input type="submit" formaction="" class="btn btn-primary btn-small" value="导出Excel" />*@
<a class="btn btn-primary btn-small" href="@("/WarehouseInOut/ExportTable?" + Request.QueryString.ToString())">导出Excel</a>
后端代码
public void ExportWarehouseInOutDetailTable(long warehouseInOutId)
{
Response.Clear();
if (warehouseInOutId <= )
{
Response.Write("<script>confirm('没有查询到任何数据!')</script>");
return;
}
var model = _service.DetailIncludeDetailsAndGoods(warehouseInOutId);
if (model.IsNull() || model.Id <= )
{
Response.Write("<script>confirm('没有查询到任何数据!')</script>");
return;
} Response.ContentType = "application/vnd.ms-excel";
string fileName = string.Format("{0}_{1}.xls", model.StorageStatus.GetEnumDesc(), model.SerialId);
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName + ".xls"));
Response.Clear();
var workBook = _service.ExportWarehouseInOutDetailTable(model);
var ms = new MemoryStream();
workBook.Write(ms);
Response.BinaryWrite(ms.GetBuffer());
#region 旧代码返回File,已注释
////if(warehouseInOutId<=0) //返回空File
//var model = _service.DetailIncludeDetailsAndGoods(warehouseInOutId);
////if(model.IsNull() || model.Id <= 0) //返回空File
//var workBook = _service.ExportNpoiExcelWookBook(model);
//var ms = new MemoryStream();
//workBook.Write(ms);
//ms.Seek(0, SeekOrigin.Begin);
//方法指定返回FileResult
//return File(ms, "application/vnd.ms-excel"
#endregion, string.Format("{0}_{1}.xls", model.StorageStatus.GetEnumDesc(), model.SerialId));
} public HSSFWorkbook ExportWarehouseInOutDetailTable(WarehouseInOutContract model)
{
HSSFWorkbook workBook = new HSSFWorkbook();
ISheet sheet1 = workBook.CreateSheet("Sheet1");
ICellStyle centerCellstyle = workBook.CreateCellStyle();
centerCellstyle.VerticalAlignment = VerticalAlignment.Center;
centerCellstyle.Alignment = HorizontalAlignment.Center;
ICellStyle centerBoldCellstyle = centerCellstyle;
HSSFFont font = (HSSFFont) workBook.CreateFont();
font.Boldweight = (short)FontBoldWeight.Bold;
centerBoldCellstyle.SetFont(font);
int rowNumIndex = ; IRow row1 = sheet1.CreateRow(rowNumIndex++);
var cellTitle = row1.CreateCell();
cellTitle.SetCellValue(model.StorageStatus.GetEnumDesc());
cellTitle.CellStyle = centerBoldCellstyle; //样式必须要单独指定到cell元素,直接指定到行无效:row1.RowStyle = centerBoldCellstyle;
sheet1.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(, , , )); IRow row2 = sheet1.CreateRow(rowNumIndex++);
row2.CreateCell().SetCellValue("单据编号"); //TODO:待实现 GetDisplayAttrName(model.SerialId)
row2.CreateCell().SetCellValue(model.SerialId);
row2.CreateCell().SetCellValue("创建时间");
row2.CreateCell().SetCellValue(model.CreatedOn);
if (!model.IsOutWarehouse)
{
row2.CreateCell().SetCellValue("本批次总价");
row2.CreateCell().SetCellValue(decimal.ToDouble(model.TotalPrice));
} IRow row3 = sheet1.CreateRow(rowNumIndex++);
row3.CreateCell().SetCellValue("仓库管理员姓名");
row3.CreateCell().SetCellValue(model.WarehouseHandlerName);
row3.CreateCell().SetCellValue("采购员或领料员");
row3.CreateCell().SetCellValue(model.OutHandlerName);
row3.CreateCell().SetCellValue("所属仓库名称");
row3.CreateCell().SetCellValue(model.WarehouseAreaName); IRow row4 = sheet1.CreateRow(rowNumIndex++);
row4.CreateCell().SetCellValue("物品名称");
row4.CreateCell().SetCellValue("单位名称");
row4.CreateCell().SetCellValue("类型名称");
row4.CreateCell().SetCellValue("总价");
row4.CreateCell().SetCellValue("数量");
row4.CreateCell().SetCellValue("单价");
row4.CreateCell().SetCellValue("备注");
foreach (var det in model.WarehouseInOutDetails)
{
IRow rowDet = sheet1.CreateRow(rowNumIndex++);
rowDet.CreateCell().SetCellValue(det.Goods.Name);
rowDet.CreateCell().SetCellValue(det.Goods.UnitName);
rowDet.CreateCell().SetCellValue(det.Goods.TypeName);
rowDet.CreateCell().SetCellValue(decimal.ToDouble(det.TotalPrice));
rowDet.CreateCell().SetCellValue(det.Quantity);
rowDet.CreateCell().SetCellValue(decimal.ToDouble(det.UnitPrice));
rowDet.CreateCell().SetCellValue(det.Remark);
}
return workBook;
}
参考网站:
http://www.cnblogs.com/jiekzou/p/4766701.html
https://dotblogs.com.tw/killysss/archive/2010/01/27/13344.aspx
http://www.cnblogs.com/xwgli/archive/2013/05/03/3057824.html
http://www.cnblogs.com/bubugao/p/Excel.html
http://www.cnblogs.com/Lxy-/p/5791721.html
ASP.NET MVC导出excel npoi的更多相关文章
- ASP.NET MVC导出excel
ASP.NET MVC导出excel 要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式 ...
- ASP.NET MVC导出excel(数据量大,非常耗时的,异步导出)
要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...
- asp.net mvc 导出Excel
[HttpGet] public void ExportNissan(string CheckListNo) { JObject queryParam; if (CheckListNo == null ...
- mvc导出excel 之 新
前段时间做的mvc导出excel 老大说要进行优化,我原来导出是用npoi插件进行导出,格式是将数据放入到datatable中,然后进行导出. 说要优化的时候就想着将datatable数据导出格式改为 ...
- Mvc 导出 Excel
Mvc 导出 Excel 之前接触过Webform,winfrom 的导出Excel方法 ,优点:省事.缺点:服务器必须安装Office 这几天做项目 和 大牛学习了一下 新的方法,自己加以总结.希望 ...
- asp.net中导出Excel的方法
一.asp.net中导出Excel的方法: 本文转载 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出 ...
- ASP.net中导出Excel的简单方法介绍
下面介绍一种ASP.net中导出Excel的简单方法 先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧) <div id="export" runat=&quo ...
- 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法
-----转载:http://blog.csdn.net/sgear/article/details/7663502 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格 ...
- C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation
C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation 后台添加以下方法:/// <summa ...
随机推荐
- How can R and Hadoop be used together?
Referer: http://www.quora.com/How-can-R-and-Hadoop-be-used-together/answer/Jay-Kreps?srid=OVd9&s ...
- 物联网架构成长之路(16)-SpringCloud从入门到吹水
1.前言 Spring Cloud 现在比较流行,版本更新也是蛮快的,网上资料也是很多.很多参考网上资料就可以学到了.这里给个 http://blog.csdn.net/forezp/article/ ...
- 5. BERT算法原理解析
1. 语言模型 2. Attention Is All You Need(Transformer)算法原理解析 3. ELMo算法原理解析 4. OpenAI GPT算法原理解析 5. BERT算法原 ...
- 【九天教您南方cass 9.1】 13 等高线法计算土方量
同学们大家好,欢迎收看由老王测量上班记出品的cass9.1视频课程 我是本节课主讲老师九天. 我们讲课的教程附件也是共享的,请注意索取 在测量空间中. [点击索取cass教程]5元立得 (给客服说暗号 ...
- 【iCore4 双核心板】4.3寸液晶模块程序发布
一.说明 1.本资料包程序包含两部分,"CAPTURE"为液晶显示截图,"tft4.3"为ARM程序. 2.此程序只适合iCore4液晶模块. 3.iCore4 ...
- git排除常用配置,svn与git共存时.gitignore配置
#idea与myeclipse配置文件全部排除 *.class #package file*.war*.ear #kdiff3 ignore*.orig #maven ignoretarget/ #e ...
- Java8学习笔记(四)--接口增强
增强点 静态方法 public interface InterfacePlus { void run(); static Date createDate(){ return new Date(); } ...
- python 守护进程,监控进程
守护进程代码: import time, os import subprocess def run(): while True: taskList = os.popen('tasklist').rea ...
- flume 1.8 安装部署
环境 centos:7.2 JDK:1.8 Flume:1.8 一.Flume 安装 1) 下载 wget http://mirrors.tuna.tsinghua.edu.cn/apa ...
- Linux权限详解 命令之 chmod:修改权限
权限简介 Linux系统上对文件的权限有着严格的控制,用于如果相对某个文件执行某种操作,必须具有对应的权限方可执行成功. Linux下文件的权限类型一般包括读,写,执行.对应字母为 r.w.x. Li ...