07版java poi 导出内存溢出终极解决方案

03版用HSSFWorkbook最多只能导入六万五行的数据,07版excel用SXSSFWorkbook 可以导出几百万行的数据。

需要poi最新jar包:
1.poi-3.10-FINAL.jar
2.poi-3.8-20120326.jar
3.poi-ooxml-3.10-FINAL.jar
4.poi-ooxml-3.8-20120326.jar
5.poi-ooxml-schemas-3.10-FINAL.jar
6.poi-ooxml-schemas-3.8-20120326.jar
7.xmlbeans-2.6.0.jar

 Workbook wb = new SXSSFWorkbook(100);//内存放100条数据
Row row = null;
Cell cell = null;
Sheet sheet=(SXSSFSheet) wb.createSheet("sheet名字");
row =  sheet.createRow("行的位置");
cell = row.createCell("列的位置");
cell.setCellValue"写入内容");
cell.setCellStyle("写入样式");
sheet.setColumnWidth("列的位置", 5000);
//声明行
int rownum=0;
int rowaccess=100;//100条
//循环省略每循环一次rownum++
if(rownum%rowaccess==0){
                try {
                    ((SXSSFSheet) sheet).flushRows();
                } catch (IOException e) {
                    e.printStackTrace();
                }
             }

这样写入excel时每写入100条数据清除一下缓存,实测40w数据可导出不报内存溢出。

你可能感兴趣的:(07版java poi 导出内存溢出终极解决方案)