问题描述
- 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(); } } }
解决方案
java使用POI的导入导出
java中使用poi导出Excel详解
java中使用poi导出Excel详解
时间: 2024-10-26 05:57:13