2 kfq3312 kfq3312 于 2016.04.20 13:49 提问

java 使用POI导出大数据,服务器内存不释放

RT,我用java 使用POI导出大数据,
数据是可以导出来。但是服务器的内存一直不释放。导一次 内存就会增加100M-2G 视数据量大小。当超过JVM设置的15G时,服务就会瘫痪掉,无法访问、
主要代码如下,

 MapList list  = db.query(SQL);
        String EXCELNAME = (new StringBuilder(String.valueOf(unitId))).toString();
         HSSFWorkbook workbook = new HSSFWorkbook();
            //createSheet(excel工作表名)
            HSSFSheet sheet = workbook.createSheet(EXCELNAME);
            //下面是设置excel表中标题的样式
            HSSFCellStyle title_style = workbook.createCellStyle();
            title_style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
            title_style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            title_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            title_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            title_style.setBorderRight(HSSFCellStyle.BORDER_THIN);
            title_style.setBorderTop(HSSFCellStyle.BORDER_THIN);
            title_style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            HSSFFont title_font = workbook.createFont();
            title_font.setColor(HSSFColor.VIOLET.index);
            title_font.setFontHeightInPoints((short) 12);
            title_font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            title_style.setFont(title_font);
            //内容的样式
            HSSFCellStyle content_style = workbook.createCellStyle();
            content_style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
            content_style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            content_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            content_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            content_style.setBorderRight(HSSFCellStyle.BORDER_THIN);
            content_style.setBorderTop(HSSFCellStyle.BORDER_THIN);
            content_style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            content_style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFFont content_font = workbook.createFont();
            content_font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
            content_style.setFont(content_font);
            //填充标题内容
            HSSFRow row = sheet.createRow(0);
            for (int i = 0; i < TITLES.length; i++) {
                //设置标题的宽度自适应
                sheet.setColumnWidth(i, TITLES[i].getBytes().length * 2 * 256);
                HSSFCell cell = row.createCell(i);
                cell.setCellStyle(title_style);
                HSSFRichTextString text = new HSSFRichTextString(TITLES[i]);
                cell.setCellValue(text);
            }
               for (int i = 0; i < list.size(); i++) {
                   row = sheet.createRow( i + 1);  
                   for( int val=0;val<TITLES.length;val++){
                       HSSFCell cell = row.createCell(val);
                        cell.setCellStyle(content_style);
                        HSSFRichTextString richString = new HSSFRichTextString(list.getRow(i).get(val));
                        cell.setCellValue(richString);
                   }
           }
            response.reset();
            response.setContentType("application/vnd.ms-excel ;charset=" + 
                      LocaleConfig.getEncoding());
            response.setHeader("Content-disposition", "attachment;filename="
                  +EXCELNAME   + ".xls");
            response.addHeader("Cache-Control", "no-cache");
            response.addHeader("Cache-Control", "no-store");
            OutputStream ouputStream = response.getOutputStream();
            try {
                workbook.write(ouputStream);
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                    if(ouputStream!=null){
                         ouputStream.flush();
                         ouputStream.close();
                    }

            }
    }

2个回答

CSDNXIAON
CSDNXIAON   2016.04.21 21:22

java使用POI的导入导出
java中使用poi导出Excel详解
java中使用poi导出Excel详解
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

xxmvzeetbb
xxmvzeetbb   2016.10.05 23:53

0; i < list.size(); i++) {
row = sheet.createRow( i + 1);

for( int val=0;val<TITLES.length;val++){
HSSFCell cell = row.createCell(val);
cell.setCellStyle(content_style);
HSSFRichTextString richString = new HSSFRichTextString(list.getRow(i).get(val));
cell.setCellValue(richString);
}
}
response.reset();
response.setContentType("application/vnd.ms-excel ;charset=" +
LocaleConfig.getEncoding());
response.setHeader("Content-disposition", "attachment;filename="
+EXCELNAME + ".xls");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "no-store");
OutputStream ouputStream = response.getOutputStream();
try {
workbook.write(ouputStream);
} catch (Exception e) {

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!