使用SXSSF POI编写大型Excel工作表

我正在使用POI()的stream式工作表编写一个大的Excel工作表(超过一百万条logging),但是我得到了java.lang.OutOfMemoryError: Java heap space 。 这是我的代码:

  public void exportExcel() { try { SXSSFWorkbook workbook = new SXSSFWorkbook(); Sheet sheet = workbook.createSheet(); List<Oject> list = dao.getList(); Cell cell = null; if (!list.isEmpty()) { Row row = sheet.createRow(0); cell = row.createCell(0); cell.setCellValue("s1"); cell = row.createCell(1); cell.setCellValue("s2"); cell = row.createCell(2); cell.setCellValue("s3"); cell = row.createCell(3); cell.setCellValue("s4"); cell = row.createCell(4); cell.setCellValue("s5"); cell = row.createCell(5); cell.setCellValue("s6"); cell = row.createCell(6); cell.setCellValue("s7"); cell = row.createCell(7); cell.setCellValue("s8"); cell = row.createCell(8); cell.setCellValue("s9"); cell = row.createCell(9); cell.setCellValue("s10"); cell = row.createCell(10); cell.setCellValue("s11"); cell = row.createCell(11); cell.setCellValue("s12"); cell = row.createCell(12); cell.setCellValue("s13"); cell = row.createCell(13); cell.setCellValue("s14"); cell = row.createCell(14); cell.setCellValue("s15"); cell = row.createCell(15); cell.setCellValue("s16"); cell = row.createCell(16); cell.setCellValue("s17"); cell = row.createCell(17); cell.setCellValue("s18"); cell = row.createCell(18); cell.setCellValue("s19"); cell = row.createCell(19); cell.setCellValue("s20"); cell = row.createCell(20); cell.setCellValue("s21"); cell = row.createCell(21); cell.setCellValue("s22"); cell = row.createCell(22); cell.setCellValue("s23"); cell = row.createCell(23); cell.setCellValue("s24"); cell = row.createCell(24); cell.setCellValue("s25"); cell = row.createCell(25); cell.setCellValue("s26"); cell = row.createCell(26); cell.setCellValue("s27"); cell = row.createCell(27); cell.setCellValue("s28"); cell = row.createCell(28); cell.setCellValue("s29"); cell = row.createCell(29); cell.setCellValue("s30"); cell = row.createCell(30); cell.setCellValue("s31"); cell = row.createCell(31); cell.setCellValue("s32"); cell = row.createCell(32); cell.setCellValue("s33"); cell = row.createCell(33); cell.setCellValue("s34"); cell = row.createCell(34); cell.setCellValue("s35"); cell = row.createCell(35); cell.setCellValue("s36"); cell = row.createCell(36); cell.setCellValue("s37"); for (int i = 0; i < list.size(); i++) { row = sheet.createRow(i + 1); cell = row.createCell(0); if (list.get(i).getOne == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getOne); } cell = row.createCell(1); if (list.get(i).getTwo() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getTwo()); } cell = row.createCell(2); if (list.get(i).getThree() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getThree()); } cell = row.createCell(3); if (list.get(i).getFour() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getFour().toString()); } cell = row.createCell(4); if (list.get(i).getFive() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getFive()); } cell = row.createCell(5); if (list.get(i).getSix() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getSix()); } cell = row.createCell(6); if (list.get(i).getSeven() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getSeven()); } cell = row.createCell(7); if (list.get(i).getEight() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getEight()); } cell = row.createCell(8); if (list.get(i).getNine() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getNine()); } cell = row.createCell(9); if (list.get(i).getTen() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getTen().toString()); } cell = row.createCell(10); if (list.get(i).getD() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getD()); } cell = row.createCell(11); if (list.get(i).getF() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getF().toString()); } cell = row.createCell(12); if (list.get(i).getV() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getV().toString()); } cell = row.createCell(13); if (list.get(i).getVa() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getVa().toString()); } cell = row.createCell(14); if (list.get(i).getFo() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getFo().toString()); } cell = row.createCell(15); if (list.get(i).getVar() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getVar().toString()); } cell = row.createCell(16); if (list.get(i).getVarm() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getVarm().toString()); } cell = row.createCell(17); if (list.get(i).getFor() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getFor().toString()); } cell = row.createCell(18); if (list.get(i).getVarp() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getVarp().toString()); } cell = row.createCell(19); if (list.get(i).getVau() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getVau().toString()); } cell = row.createCell(20); if (list.get(i).getFoi() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getFoi().toString()); } cell = row.createCell(21); if (list.get(i).getVaro() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getVaro()); } cell = row.createCell(22); if (list.get(i).getVo() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getvo().toString()); } cell = row.createCell(23); if (list.get(i).getF5() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getF5().toString()); } cell = row.createCell(24); if (list.get(i).getV5() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getV5().toString()); } cell = row.createCell(25); if (list.get(i).getVm5() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getVm5().toString()); } cell = row.createCell(26); if (list.get(i).getDo() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getDo().toString()); } cell = row.createCell(27); if (list.get(i).getDmow() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getDmow().toString()); } cell = row.createCell(28); if (list.get(i).getCut() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getCut()); } cell = row.createCell(29); if (list.get(i).getDre() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getDre().toString()); } cell = row.createCell(30); if (list.get(i).getCli() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getCli()); } cell = row.createCell(31); if (list.get(i).getDn() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getDn().toString()); } cell = row.createCell(32); if (list.get(i).getCod() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getCod()); } cell = row.createCell(33); if (list.get(i).getCk() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getCk()); } cell = row.createCell(34); if (list.get(i).getRem() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getRem().toString()); } cell = row.createCell(35); if (list.get(i).getPs() == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getPs().toString()); } cell = row.createCell(36); if (list.get(i).getTyp) == null) { cell.setCellValue(""); } else { cell.setCellValue(list.get(i).getTyp().toString()); } } } FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext() externalContext.setResponseContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"Lim_" + new Date().toString() + ".xlsx\""); workbook.write(externalContext.getResponseOutputStream()); facesContext.responseComplete(); workbook.dispose(); } catch (Exception e) { FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", null)); e.printStackTrace(); } } 

请问我需要了解问题,因为列表的大小是变化的,我的堆大小是512米

当您使用new SXSSFWorkbook();创buildstream工作簿时new SXSSFWorkbook(); ,您可以隐式指定无限制的行访问权限。 这通常不是你想写的东西。 我build议添加一个整数参数的构造函数,例如

  SXSSFWorkbook workbook = new SXSSFWorkbook(100); 

这应该大大减less内存消耗。 你甚至可以用1作为数字,只要你只按顺序写行。