您可能需要些许了解
Java中涉及的数据导出,大多都是将数据库中的数据导出到Excel文件中,当进行大量数据进行导出时很容易出现内存溢出的问题,未解决此问题,该文章介绍多种优化方案,本文重在介绍通过SXSSFWorkbook进行大量数据导出到Excel的操作。
POI之前的版本不支持大数据量处理,如果数据过多则经常报OOM错误,有时候调整JVM大小效果也不是太好。3.8版本的POI新出来了SXSSFWorkbook,可以支持大数据量的操作,只是SXSSFWorkbook只支持.xlsx格式,不支持.xls格式。
3.8版本的POI对Excel的导出操作,一般只使用 HSSFWorkbook 以及 SXSSFWorkbook,HSSFWorkbook用来处理较少的数据量,SXSSFWorkbook用来处理大数据量以及超大数据量的导出。
SXSSFWorkbook实例
List<ProjectContractInfo> list =***;
if(list!=
null && list.size() >
0){
int total = list.size();
int mus =
50000;
int avg = (total/mus)+
1;
SXSSFWorkbook wb =
new SXSSFWorkbook(
100);
CellStyle styleContent = wb.createCellStyle();
styleContent.setFillBackgroundColor((
short)
13);
styleContent.setFillForegroundColor((
short)
15);
styleContent.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
styleContent.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleContent.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
styleContent.setBorderTop(HSSFCellStyle.BORDER_THIN);
styleContent.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleContent.setBorderBottom(HSSFCellStyle.BORDER_THIN);
styleContent.setBorderLeft(HSSFCellStyle.BORDER_THIN);
Font fontContent = wb.createFont();
fontContent.setFontName(
"宋体");
fontContent.setBold(
true);
fontContent.setFontHeightInPoints((
short)
11);
styleContent.setFont(fontContent);
styleContent.setWrapText(
true);
CellStyle style= wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.WHITE.index);
style.setFillForegroundColor(HSSFColor.WHITE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
Font font = wb.createFont();
font.setFontName(
"宋体");
font.setFontHeightInPoints((
short)
11);
style.setFont(font);
style.setWrapText(
true);
CellStyle style_date = wb.createCellStyle();
DataFormat format = wb.createDataFormat();
style_date.setDataFormat(format.getFormat(
"yyyy-MM-dd"));
style_date.setFillBackgroundColor(HSSFColor.WHITE.index);
style_date.setFillForegroundColor(HSSFColor.WHITE.index);
style_date.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style_date.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style_date.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style_date.setBorderTop(HSSFCellStyle.BORDER_THIN);
style_date.setBorderRight(HSSFCellStyle.BORDER_THIN);
style_date.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style_date.setBorderLeft(HSSFCellStyle.BORDER_THIN);
Font font_date = wb.createFont();
font_date.setFontName(
"宋体");
font_date.setFontHeightInPoints((
short)
11);
style_date.setFont(font_date);
style_date.setWrapText(
true);
for(
int m =
0; m < avg; m++){
SXSSFSheet sheet = wb.createSheet(
"项目信息"+(m+
1));
int sheet_row =
0;
sheet.setColumnWidth(
0,
8000);
sheet.setColumnWidth(
1,
8000);
sheet.setColumnWidth(
2,
8000);
sheet.setColumnWidth(
3,
8000);
sheet.setColumnWidth(
4,
6000);
sheet.setColumnWidth(
5,
6000);
SXSSFRow sheet_row1 = sheet.createRow(sheet_row);
sheet_row1.setHeight((
short)
400);
SXSSFCell sheetcell11 = sheet_row1.createCell(
0);
sheetcell11.setCellValue(
"项目编号");
sheetcell11.setCellStyle(styleContent);
SXSSFCell sheetcell12 = sheet_row1.createCell(
1);
sheetcell12.setCellValue(
"委托单位");
sheetcell12.setCellStyle(styleContent);
SXSSFCell sheetcell13 = sheet_row1.createCell(
2);
sheetcell13.setCellValue(
"工程名称");
sheetcell13.setCellStyle(styleContent);
SXSSFCell sheetcell14 = sheet_row1.createCell(
3);
sheetcell14.setCellValue(
"详细地点");
sheetcell14.setCellStyle(styleContent);
SXSSFCell sheetcell15 = sheet_row1.createCell(
4);
sheetcell15.setCellValue(
"签订日期");
sheetcell15.setCellStyle(styleContent);
SXSSFCell sheetcell16 = sheet_row1.createCell(
5);
sheetcell16.setCellValue(
"结算方式");
sheetcell16.setCellStyle(styleContent);
sheet_row = sheet_row+
1;
ProjectContractInfo pct =
null;
SXSSFRow sheet_rows =
null;
SXSSFCell sheetcell =
null;
for(
int n = mus*m; n < mus*(m+
1); n++){
if(n < total){
pct = list.get(n);
sheet_rows = sheet.createRow(sheet_row);
sheet_rows.setHeight((
short)
400);
sheetcell = sheet_rows.createCell(
0);
sheetcell.setCellValue(pct.getContract_no());
sheetcell.setCellStyle(style);
sheetcell = sheet_rows.createCell(
1);
sheetcell.setCellValue(pct.getTrustor());
sheetcell.setCellStyle(style);
sheetcell = sheet_rows.createCell(
2);
sheetcell.setCellValue(pct.getProject_name());
sheetcell.setCellStyle(style);
sheetcell = sheet_rows.createCell(
3);
sheetcell.setCellValue(pct.getProject_address());
sheetcell.setCellStyle(style);
sheetcell = sheet_rows.createCell(
4);
if(pct.getContract_date()!=
null){
sheetcell.setCellValue(
new SimpleDateFormat(
"yyyy-MM-dd").format(pct.getContract_date()));
}
sheetcell.setCellStyle(style_date);
sheetcell = sheet_rows.createCell(
5);
sheetcell.setCellValue(pct.getSettle_type());
sheetcell.setCellStyle(style);
sheet_row = sheet_row +
1;
pct =
null;
sheet_rows =
null;
sheetcell =
null;
}
}
}
try {
submitFlag =
null;
String fileName =
"项目信息.xlsx";
response.setContentType(
"applicatin/ms-excel");
response.setHeader(
"Content-Disposition",
"attachment;filename="+
new String(fileName.getBytes(
"gb2312"),
"iso-8859-1"));
wb.write(response.getOutputStream());
wb.close();
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
else{
map.put(
"errorMsg",
"无数据!");
return ftlURL;
}
片尾留注
*以上代码片段摘自实际开发项目
转载请注明原文地址: https://ju.6miu.com/read-668344.html