使用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 应用程序接口指南---第二节:UI---第四章 Action Bar

    第4章 Action Bar Action Bar是一个能用于确定应用程序和用户的位置,并提供给用户操作和导航模式的窗口功能.如果需要显著地展示当前用户的操作或导航,应该使用Action Bar,因为 ...

  2. python修饰器(装饰器)以及wraps

    Python装饰器(decorator)是在程序开发中经常使用到的功能,合理使用装饰器,能让我们的程序如虎添翼. 装饰器的引入 初期及问题的诞生 假如现在在一个公司,有A B C三个业务部门,还有S一 ...

  3. Git应用实践(一)

    [时间:2017-03] [状态:Open] [关键词:Git,ssh,远程仓库,git remote] 0-背景 近期在使用Git@oschina上发现以下两个问题: 我的提交有两个名和email, ...

  4. java框架篇---hibernate入门

    Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hibernate可以应用在任何使用JDB ...

  5. 解决Android微信支付官方demo运行失败

    Android微信支付官方demo运行失败,在此简单记录一下解决步骤 1.httpclient错误 官方给的demo是eclipse的,打开之后提示httpclient的错误,我知道在as下解决htt ...

  6. Jquery计算指定日期加上多少天、加多少月、加多少年的日期

    /* * 功能:实现VBScript的DateAdd功能. * 参数:interval,字符串表达式,表示要添加的时间间隔. * 参数:number,数值表达式,表示要添加的时间间隔的个数. * 参数 ...

  7. Fixed Partition Memory Management UVALive - 2238 建图很巧妙 km算法左右顶点个数不等模板以及需要注意的问题 求最小权匹配

    /** 题目: Fixed Partition Memory Management UVALive - 2238 链接:https://vjudge.net/problem/UVALive-2238 ...

  8. [转]iOS 中几种定时器 - 控制了时间,就控制了一切

    这篇文章是转载内容,原文地址:http://www.cocoachina.com/ios/20150519/11857.html?utm_source=tuicool 这里的知识点,其实在我们日常开发 ...

  9. [IR] Open Source Search Engines

    From:http://blog.csdn.net/xum2008/article/details/8740063 本文档是对现有的开源的搜索引擎的一个简单介绍 1.    Lucene Lucene ...

  10. Hadoop -- HDFS 读写数据

    一.HDFS读写文件过程 1.读取文件过程 1)       初始化FileSystem,然后客户端(client)用FileSystem的open()函数打开文件 2)       FileSyst ...