用POI创buildExcel

我正在使用Apache POI 3.7来创buildHSSF Excel。 这工作正常。 但在负载testing期间,我意识到它非常慢。 所以我Googlesearch,发现我可以使用SXSSF 。 我将现有的代码更改为XSSF 。 结果是真棒。

但是,我坚持一个情况, autorezisecolum()不能按预期运行。 它显示大内容为##### ,因为列的宽度会缩小。 我发现这是一种已经提出的bug。

现在我的观点是,是否有任何解决scheme,以便我可以使用SXSSF (对性能非常重要)与一个很好的输出。

注意:我使用的是Windows 7,JDK 1.7.09,POI-3.10.beta-2

请帮帮我。

这是我的代码:

主function :

 sxssfWorkbook = new SXSSFWorkbook(5); sxssfSheet = (SXSSFSheet) sxssfWorkbook.createSheet(sheetName); try { // TO Write Header //ew.writeHeaderRow(sheet, headerNames); ew.writeHeaderRow(sxssfSheet, headerNames); int rowNum = headerRow + 1; for(Map.Entry<String, List<Object>> columnData : columnDataMap.entrySet()){ ew.writeNonHeaderRow(sxssfSheet, columnData.getValue(), rowNum); rowNum++; } resizeXLSXColumns(sxssfSheet,rowNum-1); sxssfWorkbook.write(outputStream); outputStream.close(); public void writeHeaderRow(Sheet sheet, List<String> headerNames ) { //public void writeHeaderRow(SXSSFSheet sxssfSheet, List<String> headerNames ) { // LinkedHashMap<String,Object> mp = getFieldNames(obj); // ArrayList<String> colNames = (ArrayList<String>) getColumnNames(); try { XSSFCellStyle hCellStyle = getHeaderStyle(); SXSSFRow row = (SXSSFRow) sheet.createRow(headerRow); for (int hCellInd = 0; hCellInd < headerNames.size(); hCellInd++) { SXSSFCell cell = (SXSSFCell) row.createCell(hCellInd); cell.setCellStyle(hCellStyle); cell.setCellValue(headerNames.get(hCellInd)); //sheet.autoSizeColumn(hCellInd); } } catch (Exception e) { e.printStackTrace(); } } public void writeNonHeaderRow(SXSSFSheet sxssfSheet, List<Object> rowObj, int rowIndex) { CreationHelper createHelper = sxssfWorkbook.getCreationHelper(); try { SXSSFRow row = (SXSSFRow)sheet.createRow(rowIndex); XSSFCellStyle normalStyle = getNormalStyle(); int count = 0; for (int rCellInd = 0; rCellInd < rowObj.size(); rCellInd++) { //Cell cell = row.createCell(rCellInd); SXSSFCell cell = (SXSSFCell)row.createCell(rCellInd); cell.setCellStyle(normalStyle); Object cellData = rowObj.get(rCellInd); if (cellData != null) { if (cellData instanceof Double) { cell.setCellValue((Double) cellData); if((Double)cellData < 0){ cell.setCellStyle(getNegativeValueStyle()); }else if((Double)cellData == 0){ normalStyle.setDataFormat((short) SXSSFCell.CELL_TYPE_BLANK) ; cell.setCellStyle(normalStyle); }else cell.setCellStyle(normalStyle); } else { //normalStyle.setDataFormat((short) HSSFCell.CELL_TYPE_BLANK); cell.setCellType(SXSSFCell.CELL_TYPE_BLANK); } //sxssfSheet.autoSizeColumn(); //sxssfSheet.setColumnWidth(rCellInd, sxssfSheet.getColumnWidth(rCellInd)); //resizeXLSXColumns(sheet); } //autoResizeColumns(sxssfSheet); } catch (Exception e) { e.printStackTrace(); } } public static void resizeXLSXColumns(Sheet sheet ,int rowNum){ SXSSFRow row = (SXSSFRow)sheet.getRow(rowNum); Iterator<Cell> itr = row.cellIterator(); int max = 0; while(itr.hasNext()){ Cell cell = itr.next(); int width = sheet.getColumnWidth(cell.getColumnIndex()); if(width > max){ max = width; } //sheet.setColumnWidth(cell.getColumnIndex(),max); } while(itr.hasNext()){ Cell cell = itr.next(); sheet.setColumnWidth(cell.getColumnIndex(),max); } }