NPOI导出信息

 

public ActionResult ExportInfo(ProjectSearch projectWhere)
        {
            HSSFWorkbook hssfworkbook = new HSSFWorkbook();

            #region 定义excel表格
            ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
            IRow row1 = sheet1.CreateRow(0);
            row1.CreateCell(0).SetCellValue("姓名");
            row1.CreateCell(1).SetCellValue("身份证号");
            #endregion

            #region 填充数据
            int rownum = 1;
            long total = 0;var list = new List<Project>();//假装有数据
            var sheetIndex = 1;
            foreach (Project model in list)
            {
                //如果行数超过了excel最大限制,创建新sheet
                if ((rownum + 1) % 65535 == 0)
                {
                    sheet1 = hssfworkbook.CreateSheet("Sheet" + (++sheetIndex));
                    IRow row0 = sheet1.CreateRow(0);
                    row0.CreateCell(0).SetCellValue("姓名");
                    row0.CreateCell(1).SetCellValue("身份证号");
                    rownum = 1;
                    sheetIndex++;
                }
                IRow row3 = sheet1.CreateRow(rownum);
                row3.CreateCell(0).SetCellValue(model.Name);
                row3.CreateCell(1).SetCellValue(model.PersonalId);
                rownum++;
            }

            #endregion

            string folder = Server.MapPath("/Down/");
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            //写入到表格
            string filename2 = "信息" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
            using (FileStream fs = new FileStream(folder + filename2, FileMode.OpenOrCreate, FileAccess.Write))
            {
                hssfworkbook.Write(fs);
            }

            string url = "../Down/" + filename2;
            return Json(new { result = true, url = url });
        }    

 

上一篇:09 在oracle以及mysql当中实现分页


下一篇:oracle部分:分页查询