java poi 生成Excel 速度非常慢,求优化方案?数据才到2000左右的时候就开始变慢了。。
while(rs.next()){HSSFCellStylecellStyle=wb.createCellStyle();HSSFDataFormatformat=wb....
while(rs.next()){
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFDataFormat format = wb.createDataFormat();
cellStyle.setDataFormat(format.getFormat("@"));
HSSFRow row = sheet.createRow(i+2);
HSSFCell policyNoCell = row.createCell((short) 0);
policyNoCell.setCellStyle(cellStyle);
policyNoCell.setCellValue(new HSSFRichTextString(rs.getString(3)));
row.createCell((short) 0).setCellValue(new HSSFRichTextString(rs.getString(1)));
row.createCell((short) 1).setCellValue(new HSSFRichTextString(rs.getString(2)));
row.createCell((short) 2).setCellValue(new HSSFRichTextString(rs.getString(3)));
row.createCell((short) 3).setCellValue(new HSSFRichTextString(rs.getString(4)));
row.createCell((short) 4).setCellValue(new HSSFRichTextString(rs.getString(5)));
row.createCell((short) 5).setCellValue(new HSSFRichTextString(rs.getString(6)));
row.createCell((short) 6).setCellValue(new HSSFRichTextString(rs.getString(7)));
row.createCell((short) 7).setCellValue(new HSSFRichTextString(rs.getString(8).substring(0,9)));
row.createCell((short) 8).setCellValue(new HSSFRichTextString(rs.getString(9).substring(0,9)));
ExcelWrite.autoSetWidth(sheet,i);
i++;
} 展开
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFDataFormat format = wb.createDataFormat();
cellStyle.setDataFormat(format.getFormat("@"));
HSSFRow row = sheet.createRow(i+2);
HSSFCell policyNoCell = row.createCell((short) 0);
policyNoCell.setCellStyle(cellStyle);
policyNoCell.setCellValue(new HSSFRichTextString(rs.getString(3)));
row.createCell((short) 0).setCellValue(new HSSFRichTextString(rs.getString(1)));
row.createCell((short) 1).setCellValue(new HSSFRichTextString(rs.getString(2)));
row.createCell((short) 2).setCellValue(new HSSFRichTextString(rs.getString(3)));
row.createCell((short) 3).setCellValue(new HSSFRichTextString(rs.getString(4)));
row.createCell((short) 4).setCellValue(new HSSFRichTextString(rs.getString(5)));
row.createCell((short) 5).setCellValue(new HSSFRichTextString(rs.getString(6)));
row.createCell((short) 6).setCellValue(new HSSFRichTextString(rs.getString(7)));
row.createCell((short) 7).setCellValue(new HSSFRichTextString(rs.getString(8).substring(0,9)));
row.createCell((short) 8).setCellValue(new HSSFRichTextString(rs.getString(9).substring(0,9)));
ExcelWrite.autoSetWidth(sheet,i);
i++;
} 展开
1个回答
展开全部
发现你问题的所在了,你把过多的可重用的东西卸写在了循环内
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFDataFormat format = wb.createDataFormat();
cellStyle.setDataFormat(format.getFormat("@"));
// HSSFRow row = sheet.createRow(i+2);//这句不可重用
HSSFCell policyNoCell = row.createCell((short) 0);
policyNoCell.setCellStyle(cellStyle);
policyNoCell.setCellValue(new HSSFRichTextString(rs.getString(3)));//这句不可重用
你这个完全可以卸载循环外啊,creat这么多,肯定占内存
就算你有多个style,你也可以只需要creat一次,然后反复调用就好了
切记,以后再代码设计中,一定要考虑最大可能的重用,而不是每次需要去创造(new()、crate什么的),这是一种思维,祝你好运
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFDataFormat format = wb.createDataFormat();
cellStyle.setDataFormat(format.getFormat("@"));
// HSSFRow row = sheet.createRow(i+2);//这句不可重用
HSSFCell policyNoCell = row.createCell((short) 0);
policyNoCell.setCellStyle(cellStyle);
policyNoCell.setCellValue(new HSSFRichTextString(rs.getString(3)));//这句不可重用
你这个完全可以卸载循环外啊,creat这么多,肯定占内存
就算你有多个style,你也可以只需要creat一次,然后反复调用就好了
切记,以后再代码设计中,一定要考虑最大可能的重用,而不是每次需要去创造(new()、crate什么的),这是一种思维,祝你好运
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询