MVC+NPOI==批量导出数据

 function funOutport() {
            window.location.href = "/Store/Outport";}

 [LoginAjaxJson]
        public ActionResult Outport()
        {
            try
            {
                List<Z_Storeinformation> list = StoreinformationService.GetModels(true, a => a.StoreID, a => a.Isdel == 0);
                HSSFWorkbook excelBook = new HSSFWorkbook();
                ICellStyle style1 = excelBook.CreateCellStyle();//声明style1对象,设置Excel表格的样式
                ICellStyle style2 = excelBook.CreateCellStyle();
                //ICellStyle style3 = excelBook.CreateCellStyle();
                //IFont font = excelBook.CreateFont();
                //font.Color = IndexedColors.Red.Index;
                //style3.SetFont(font);

                style1.Alignment = HorizontalAlignment.Left;//两端自动对齐(自动换行)
                style1.VerticalAlignment = VerticalAlignment.Center;
                style2.Alignment = HorizontalAlignment.Center;
                style2.VerticalAlignment = VerticalAlignment.Center; 
                //style3.Alignment = HorizontalAlignment.Center;
                //style3.VerticalAlignment = VerticalAlignment.Center;


                //创建Excel工作表 Sheet=故障码信息
                ISheet sheet1 = excelBook.CreateSheet("Sheet1");

                #region 表头
                //给Sheet(故障码信息)添加第一行的头部标题
                IRow row1 = sheet1.CreateRow(0);
                //给标题的每一个单元格赋值
                row1.CreateCell(0).SetCellValue("店铺编号");//0
                row1.CreateCell(1).SetCellValue("店铺名称");//1
                row1.CreateCell(2).SetCellValue("店铺地址");//2
                row1.CreateCell(3).SetCellValue("省/直辖市");//3
                row1.CreateCell(4).SetCellValue("市");//4
                row1.CreateCell(5).SetCellValue("区/县");//5
                row1.CreateCell(6).SetCellValue("安装时间");//6
                row1.CreateCell(7).SetCellValue("项目经理姓名");//7
                row1.CreateCell(8).SetCellValue("项目经理手机号");//8
                row1.CreateCell(9).SetCellValue("店长");//9
                row1.CreateCell(10).SetCellValue("店长手机号");//10
                row1.CreateCell(11).SetCellValue("座机号");//11
                row1.CreateCell(12).SetCellValue("是否安装完成");//12
                row1.CreateCell(13).SetCellValue("55寸屏");//13
                row1.CreateCell(14).SetCellValue("46寸屏");//14
                row1.CreateCell(15).SetCellValue("设备A");//15
                row1.CreateCell(16).SetCellValue("设备B");//16
                row1.CreateCell(17).SetCellValue("设备C");//17
                row1.CreateCell(18).SetCellValue("设备D");//18
                row1.CreateCell(19).SetCellValue("设备E");//19
                row1.CreateCell(20).SetCellValue("经度");//20
                row1.CreateCell(21).SetCellValue("纬度");//21
                row1.CreateCell(22).SetCellValue("营业时间");//22
                row1.CreateCell(23).SetCellValue("开业时间");//23
                row1.CreateCell(24).SetCellValue("备注");//24
                #endregion

                #region 表头样式
                //初始化设置样式
                row1.GetCell(0).CellStyle = style2;
                row1.GetCell(1).CellStyle = style2; 
                row1.GetCell(2).CellStyle = style2; 
                row1.GetCell(3).CellStyle = style2; 
                row1.GetCell(4).CellStyle = style2; 
                row1.GetCell(5).CellStyle = style2; 
                row1.GetCell(6).CellStyle = style2; 
                row1.GetCell(7).CellStyle = style2; 
                row1.GetCell(8).CellStyle = style2; 
                row1.GetCell(9).CellStyle = style2; 
                row1.GetCell(10).CellStyle = style2; 
                row1.GetCell(11).CellStyle = style2; 
                row1.GetCell(12).CellStyle = style2; 
                row1.GetCell(13).CellStyle = style2; 
                row1.GetCell(14).CellStyle = style2; 
                row1.GetCell(15).CellStyle = style2; 
                row1.GetCell(16).CellStyle = style2; 
                row1.GetCell(17).CellStyle = style2; 
                row1.GetCell(18).CellStyle = style2; 
                row1.GetCell(19).CellStyle = style2; 
                row1.GetCell(20).CellStyle = style2; 
                row1.GetCell(21).CellStyle = style2; 
                row1.GetCell(22).CellStyle = style2; 
                row1.GetCell(23).CellStyle = style2; 
                row1.GetCell(24).CellStyle = style2; 
                #endregion

                #region 列宽
                //初始化设置宽度
                sheet1.SetColumnWidth(0, 10 * 256);
                sheet1.SetColumnWidth(1, 40 * 256);//初始化设置宽度 
                sheet1.SetColumnWidth(2, 60 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(3, 15 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(4, 15 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(5, 15 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(6, 12 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(7, 13 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(8, 14 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(9, 10 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(10, 14 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(11, 14 * 256);//初始化设置宽度 
                sheet1.SetColumnWidth(12, 15 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(13, 10 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(14, 10 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(15, 10 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(16, 10 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(17, 10 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(18, 10 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(19, 10 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(20, 14 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(21, 14 * 256);//初始化设置宽度 
                sheet1.SetColumnWidth(22, 15 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(23, 12 * 256);//初始化设置宽度
                sheet1.SetColumnWidth(24, 10 * 256);//初始化设置宽度 
                #endregion

                #region 设置数据
                for (int i = 0; i < list.Count; i++)
                {
                    //sheet1.CreateRow(i).
                    //创建行
                    IRow rowTemp = sheet1.CreateRow(i + 1);
                    rowTemp.Height = 12 * 20;
                    //店铺编号
                    rowTemp.CreateCell(0).SetCellValue(list[i].StoreID);
                    //店铺名称
                    rowTemp.CreateCell(1).SetCellValue(list[i].StoreName);
                    //店铺地址
                    rowTemp.CreateCell(2).SetCellValue(list[i].StoreAddress);
                    //省/直辖市
                    rowTemp.CreateCell(3).SetCellValue(list[i].Province);
                    //市
                    rowTemp.CreateCell(4).SetCellValue(list[i].City);
                    //区/县
                    rowTemp.CreateCell(5).SetCellValue(list[i].Area);
                    //安装时间
                    rowTemp.CreateCell(6).SetCellValue(list[i].InstallDate.ToString() == "" ? "" : Convert.ToDateTime(list[i].InstallDate).ToString("yyyy-MM-dd"));
                    //项目经理姓名
                    rowTemp.CreateCell(7).SetCellValue(list[i].ProManagerName);
                    //项目经理手机号
                    rowTemp.CreateCell(8).SetCellValue(list[i].ProManagerPhone);
                    //店长
                    rowTemp.CreateCell(9).SetCellValue(list[i].StoreManagerName);
                    //店长手机号
                    rowTemp.CreateCell(10).SetCellValue(list[i].StoreManagerPhone);
                    //座机号
                    rowTemp.CreateCell(11).SetCellValue(list[i].AirlineNumber);
                    //是否安装完成
                    rowTemp.CreateCell(12).SetCellValue(list[i].Finished==1?"是":"否");
                    //55寸屏
                    rowTemp.CreateCell(13).SetCellValue(list[i].ScreenSize55.ToString());
                    //46寸屏
                    rowTemp.CreateCell(14).SetCellValue(list[i].ScreenSize46.ToString());
                    //设备A
                    rowTemp.CreateCell(15).SetCellValue(list[i].EquipmentA.ToString());
                    //设备B
                    rowTemp.CreateCell(16).SetCellValue(list[i].EquipmentB.ToString());
                    //设备C
                    rowTemp.CreateCell(17).SetCellValue(list[i].EquipmentC.ToString());
                    //设备D
                    rowTemp.CreateCell(18).SetCellValue(list[i].EquipmentD.ToString());
                    //设备E
                    rowTemp.CreateCell(19).SetCellValue(list[i].EquipmentE.ToString());
                    //经度
                    rowTemp.CreateCell(20).SetCellValue(list[i].PointX.ToString());
                    //纬度
                    rowTemp.CreateCell(21).SetCellValue(list[i].PointY.ToString());
                    //营业时间
                    rowTemp.CreateCell(22).SetCellValue(list[i].BusinessDate);
                    //开业时间
                    rowTemp.CreateCell(23).SetCellValue(list[i].OpenDate.ToString()==""?"":Convert.ToDateTime(list[i].OpenDate).ToString("yyyy-MM-dd"));
                    //备注
                    rowTemp.CreateCell(24).SetCellValue(list[i].Remark);

                    rowTemp.GetCell(0).CellStyle = style1;
                    rowTemp.GetCell(1).CellStyle = style1;
                    rowTemp.GetCell(2).CellStyle = style1;
                    rowTemp.GetCell(3).CellStyle = style1;
                    rowTemp.GetCell(4).CellStyle = style1;
                    rowTemp.GetCell(5).CellStyle = style1;
                    rowTemp.GetCell(6).CellStyle = style1;
                    rowTemp.GetCell(7).CellStyle = style1;
                    rowTemp.GetCell(8).CellStyle = style1;
                    rowTemp.GetCell(9).CellStyle = style1;
                    rowTemp.GetCell(10).CellStyle = style1;
                    rowTemp.GetCell(11).CellStyle = style1;
                    rowTemp.GetCell(12).CellStyle = style1;
                    rowTemp.GetCell(13).CellStyle = style1;
                    rowTemp.GetCell(14).CellStyle = style1;
                    rowTemp.GetCell(15).CellStyle = style1;
                    rowTemp.GetCell(16).CellStyle = style1;
                    rowTemp.GetCell(17).CellStyle = style1;
                    rowTemp.GetCell(18).CellStyle = style1;
                    rowTemp.GetCell(19).CellStyle = style1;
                    rowTemp.GetCell(20).CellStyle = style1;
                    rowTemp.GetCell(21).CellStyle = style1;
                    rowTemp.GetCell(22).CellStyle = style1;
                    rowTemp.GetCell(23).CellStyle = style1;
                    rowTemp.GetCell(24).CellStyle = style1;
                }
                #endregion

                //输出的文件名称
                string fileName = "店铺信息" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff") + ".xls";
                //把Excel转为流,输出
                //创建文件流
                MemoryStream bookStream = new MemoryStream();
                //将工作薄写入文件流
                excelBook.Write(bookStream);
                 
                //输出之前调用Seek(偏移量,游标位置) 把0位置指定为开始位置
                bookStream.Seek(0, SeekOrigin.Begin);
                return File(bookStream, "application/vnd.ms-excel", fileName);

                //Response.ContentType = "application/vnd.ms-excel";

                //fileName = HttpUtility.UrlEncode(fileName);
                //Response.Charset = "GB2312";
                //Response.AddHeader("Content-Disposition", "attachment; fileName=" + fileName);
                //Response.ContentEncoding = Encoding.Default;
               // return Response.OutputStream;
                //return Json(new AjaxResult { success = true, msg = "", showMsg = "操作失败" ,data= Response.OutputStream });
                //Stream对象,文件类型,文件名称
                //return 
            }
            catch (Exception ex)
            {
                return Json(new AjaxResult { success = false, msg = ex.Message.ToString(), showMsg = "操作失败" });
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值