asp.net mvc html 表格导出excel

 /// <summary>
         /// 导出流向excel
         /// </summary>
         /// <returns></returns>
         public FileResult  ExportDirectionExcel()
         {

             //创建Excel文件的对象
             NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
             //添加一个sheet
             NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");
             //给sheet1添加第一行的头部标题
             NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
             row1.CreateCell(0).SetCellValue("商品编号");
             row1.CreateCell(1).SetCellValue("名称");
             row1.CreateCell(2).SetCellValue("规格");
             row1.CreateCell(3).SetCellValue("包装单位");
             row1.CreateCell(4).SetCellValue("生产厂家");
             row1.CreateCell(5).SetCellValue("日期");
             row1.CreateCell(6).SetCellValue("单位名称");
             row1.CreateCell(7).SetCellValue("数量");
             row1.CreateCell(8).SetCellValue("单价");
             row1.CreateCell(9).SetCellValue("金额");
             row1.CreateCell(10).SetCellValue("批号");
             row1.CreateCell(11).SetCellValue("有效期");
             row1.CreateCell(12).SetCellValue("生产日期");
             //将数据逐步写入sheet1各个行

                 for (int i = 0; i < directionList2.Count; i++)
                 {
                     NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
                     rowtemp.CreateCell(0).SetCellValue(directionList2[i].Spbh.ToString());
                     rowtemp.CreateCell(1).SetCellValue(directionList2[i].Spmch.ToString());
                     rowtemp.CreateCell(2).SetCellValue(directionList2[i].Shpgg.ToString());
                     rowtemp.CreateCell(3).SetCellValue(directionList2[i].Dw.ToString());
                     rowtemp.CreateCell(4).SetCellValue(directionList2[i].Shengccj.ToString());
                     rowtemp.CreateCell(5).SetCellValue(directionList2[i].Rq.ToString());
                     rowtemp.CreateCell(6).SetCellValue(directionList2[i].Dwmch.ToString());
                     rowtemp.CreateCell(7).SetCellValue(directionList2[i].Shl.ToString());
                     rowtemp.CreateCell(8).SetCellValue(directionList2[i].Dj.ToString());
                     rowtemp.CreateCell(9).SetCellValue(directionList2[i].Je.ToString());
                     rowtemp.CreateCell(10).SetCellValue(directionList2[i].Pihao.ToString());
                     rowtemp.CreateCell(11).SetCellValue(directionList2[i].Sxrq.ToString());
                     rowtemp.CreateCell(12).SetCellValue(directionList2[i].Baozhiqi.ToString());

                 }
                 System.IO.MemoryStream ms = new System.IO.MemoryStream();
                 book.Write(ms);
                 ms.Seek(0, SeekOrigin.Begin);

                 string dateTime = DateTime.Now.ToString("yyyy-MM-dd");

                 string fileName = "查询" + dateTime + ".xls";
                 return File(ms, "application/vnd.ms-excel", fileName);

             }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值