登录 注册
当前位置:主页 > 资源下载 > 50 > 使用npoi实现Excel导出的分页功能

使用npoi实现Excel导出的分页功能

  • 更新:2024-10-09 17:59:57
  • 大小:2KB
  • 推荐:★★★★★
  • 来源:网友上传分享
  • 类别:其它 - 网络技术
  • 格式:TXT

资源介绍

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