使用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的更多相关文章

  1. ASP.NET MVC导出excel

    ASP.NET MVC导出excel 要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式 ...

  2. ASP.NET MVC导出excel(数据量大,非常耗时的,异步导出)

    要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...

  3. asp.net mvc 导出Excel

    [HttpGet] public void ExportNissan(string CheckListNo) { JObject queryParam; if (CheckListNo == null ...

  4. mvc导出excel 之 新

    前段时间做的mvc导出excel 老大说要进行优化,我原来导出是用npoi插件进行导出,格式是将数据放入到datatable中,然后进行导出. 说要优化的时候就想着将datatable数据导出格式改为 ...

  5. Mvc 导出 Excel

    Mvc 导出 Excel 之前接触过Webform,winfrom 的导出Excel方法 ,优点:省事.缺点:服务器必须安装Office 这几天做项目 和 大牛学习了一下 新的方法,自己加以总结.希望 ...

  6. asp.net中导出Excel的方法

    一.asp.net中导出Excel的方法: 本文转载 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出 ...

  7. ASP.net中导出Excel的简单方法介绍

    下面介绍一种ASP.net中导出Excel的简单方法 先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧) <div id="export" runat=&quo ...

  8. 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法

    -----转载:http://blog.csdn.net/sgear/article/details/7663502 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格 ...

  9. C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation

    C# asp.net中导出Excel表时总出现"只能在执行 Render() 的过程中调用 RegisterForEventValidation 后台添加以下方法:/// <summa ...

随机推荐

  1. 开发第一个flutter程序 hello world

    上一篇咱们配置了flutter的基本环境之后,那么本篇就来讲一下如何开发第一个小应用hello world 双击打开 android studio 选择plugins 找到 fultter 并且下载安 ...

  2. 基于mindwave脑电波进行疲劳检测算法的设计(2)

    上文讲到的是保证硬件的接通.接下来是用C语言在它提供的API接口进行连接. 在网盘中下载MindSet Development Tools这个开发包.这个目录下MindSet Development ...

  3. rman list命令

    rman list命令   List command example 可以用于查看backup,copy,archivelog等 01 list incarnation================ ...

  4. Android——Android和SVN::::SVN+delete项目

    SVN使用笔记(比较详细) http://www.cnblogs.com/merray/p/4182380.html 删除项目 http://jingyan.baidu.com/article/c74 ...

  5. 关于ECMP 等价路由

    1.ECMP简介 Equal-CostMultipathRouting,等价多路径.即存在多条到达同一个目的地址的相同开销的路径.当设备支持等价路由时,发往该目的 IP 或者目的网段的三层转发流量就可 ...

  6. 集群介绍 keepalived介绍 用keepalived配置高可用集群

    集群介绍 • 根据功能划分为两大类:高可用和负载均衡 • 高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务 • 实现高可用的开源软件有:heartb ...

  7. SpringSecurity兑现多登录成功页面和登录成功返回被拦截界面

    SpringSecurity实现多登录成功页面和登录成功返回被拦截界面 使用SrpingSceurity作为认证和授权的安全框架可以省下很多基础工作. 具体可以参考SpringSecurity,这里不 ...

  8. gradle教程 [原创](eclipse/ADT下 非插件 非Android Studio/AS)纯手打 第二篇:gradle简单实战

    一个bug 一个脚印的叫你们用gradle. 1介于网络上的很多资料都是老的 不适用与现在的新版本gradle 尤其是有些gradle方法改名了老的用不了 2介于网上都是粘贴复制并且零碎我很蛋疼啊,走 ...

  9. [Object Tracking] Contour Detection through Tensorflow running on smartphone

    From: 手机端运行卷积神经网络的一次实践 -- 基于 TensorFlow 和 OpenCV 实现文档检测功能 貌似不错的东西:移动端视觉识别模型:MobileNets Holistically- ...

  10. Android Autosizing TextViews

    https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview  Autosizing TextView ...