天天看點

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);

             }

繼續閱讀