使用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. Android查询不到电话号码解决方法

    貌似联系人有三个数据库,且不同步,另外也有可能是版本问题. 解决方案:https://github.com/codinguser/android_contact_picker 接下来会对其进行一些改造 ...

  2. 【Python】将python3.6软件的py文件打包成exe程序

    下载pyinstaller pyinstaller 改变图标 pyinstaller -F --icon=my.ico xxx.py 采用命令行操作的办法 在cmd命令行中,输入代码: 首先,前往Py ...

  3. .so.x不是符号连接

    去到这个目录下查看,会发现有多个.so.x文件.x是版本号的区别.将它们动态软连接在一起就可以了 参考这个博客

  4. redis竞汰数据同步问题解决

    Redis 面试的时候遇到过问Redis是如何解决“竞态条件”的,相关知识点总结一下. 乐观锁 所谓竞态条件,举个例子,一个代表点击数的数值hitcount,每个客户点击一次则+1. 没有事务的时候, ...

  5. TCP拥塞控制-慢启动、拥塞避免、快重传、快启动

    一般原理:发生拥塞控制的原因:资源(带宽.交换节点的缓存.处理机)的需求>可用资源. 作用:拥塞控制就是为了防止过多的数据注入到网络中,这样可以使网络中的路由器或者链路不至于过载.拥塞控制要做的 ...

  6. 【转】在Win7的IIS上搭建FTP服务及用户授权

    [转]在Win7的IIS上搭建FTP服务及用户授权 [转]在Win7的IIS上搭建FTP服务及用户授权 FTP服务 FTP是文件传输协议(File Transfer Protocol)的简称,该协议属 ...

  7. Android控件源码分析--AndroidResideMenu菜单

    说明 早上看到一篇文章介绍了ResideMenu得使用,这是一个类似SlidingMenu的控件,感觉有点高尚大,反正我之前没见过,本着凑热闹的好奇心,立马clone把玩下,项目地址奉上: https ...

  8. spring入门常见的问题及解决办法

    在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with name 'helloServi ...

  9. java对象与map对象相互转换

    /** * 使用org.apache.commons.beanutils进行转换 */ class A { public static Object mapToObject(Map<String ...

  10. Mybatis(二)基于注解的入门实例

    前言 上一篇简单的介绍了Mybatis的概念和基于XML来实现数据库的CRUD,这篇给大家实现基于注解的CRUD. 一.初始搭建 在基于注解当中前四步和上一篇基于XML是一样的,分别是: 1)创建数据 ...