npoi导出excel分页功能

时间:2020-12-19 10:51:18
【文件属性】:
文件名称:npoi导出excel分页功能
文件大小:2KB
文件格式:TXT
更新时间:2020-12-19 10:51:18
npoi private void ToExcel(HttpContext context, string TempletFileName//模版文件, string ReportFileName//导出文件, DataTable dt2) { //模板文件 //string TempletFileName = context.Server.MapPath(TempletFileName); //导出文件 //string ReportFileName = context.Server.MapPath("out.xls"); FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read); HSSFWorkbook hssfworkbook = new HSSFWorkbook(file); //XSSFWorkbook hssfworkbook = new XSSFWorkbook(file); //HSSFSheet ws = (HSSFSheet)hssfworkbook.GetSheetAt(0);//GetSheet("Sheet1"); HSSFSheet ws2 = null; int rowsCount = dt2.Rows.Count; int columnCount = dt2.Columns.Count; int num = 60000; int row = 0; if (rowsCount > 0) { int sheetCount = rowsCount > num ? (int)Math.Ceiling((double)(rowsCount / num)) : 0; for (int i = 0; i <= sheetCount; i++) { try { ws2 = (HSSFSheet)hssfworkbook.GetSheetAt(i); } catch { ws2 = (HSSFSheet)hssfworkbook.CreateSheet("Sheet" + (i + 1)); } int strRows = i * num; int endRows = num > rowsCount - (num * i) ? rowsCount : (i + 1) * num; NPOI.SS.UserModel.IRow row1 = null; for (int j = strRows; j < endRows; j++) { row++; int m = 0; if (i == 0) { m = 4; } row1 = ws2.CreateRow(row + m); row1.HeightInPoints = 25;//行高 for (int k = 0; k < columnCount; k++) { ICell cell = row1.CreateCell(k); cell.SetCellValue(dt2.Rows[j][k].ToString()); cell.CellStyle.Alignment = HorizontalAlignment.Center; } } row = 0; ws2.ForceFormulaRecalculation = true; using (FileStream fs = new FileStream(ReportFileName, FileMode.Append, FileAccess.Write)) { hssfworkbook.Write(fs); } } }

网友评论