使用Apache POI编写包含60多列的大型Excel工作表

时间:2016-01-25 09:45:16

标签: java excel apache-poi

我正在尝试使用从数据库中提取的数据创建多个Excel工作表。每个Excel工作表包含60多列和约5万条记录(可能会有所不同)。问题是系统花了很多时间(5分钟以上),然后以java.lang.OutOfMemoryError: GC overhead limit exceeded例外结束。

我尝试将列数减少到只有6,并且周转时间有了很大的改善。

这是一个生成Excel工作表字节数组的代码:

int rowIndex = 0;
while (iterator.hasNext()) {
        List<CustomCellDataBean> cellData = iterator.next();

        // Insert generic data
        Row dataContentRow = sheet.createRow((short) rowIndex);

        for (int counter = 0; counter < cellData.size(); counter++) {
            CustomCellDataBean cd = cellData.get(counter);
            if (cd.getValue() != null) {
            // switch case based on the datatype of the cell
                switch (cd.getType()) {
                }
            }
        }
        rowIndex++;
}
// write to ByteArrayOutputStream and return the array of bytes

提到了几个SO问题,但无法找出有用的东西。想知道我是否应该尝试以解决这个问题。

2 个答案:

答案 0 :(得分:3)

如果没有进一步的信息,我只能猜测你真正的问题。但我可以告诉你,apache poi可以创建超过1000列的excel表单和超过20k行的颜色,样式和内容(已经完成)。

确保使用apache.poi API

的流式传输API

org.apache.poi.xssf.streaming

这是apache的演示

Big Grid Demo

<强>更新

正如我在链接到你的演示中所说的那样,我应该使用新的SXSSF用户模型(如果我记得正确的话,我会使用它),因为它会为你处理所有的流媒体内容; - )

SXSSF (Streaming Usermodel API)

答案 1 :(得分:1)

可能看起来有些倒退,但我更喜欢在使用带POI的大型数据集时手动构建工作表。这是我使用的帮助程序类,可以帮助您入门:

public class Worksheet {
private static Logger logger = Logger.getLogger(Worksheet.class);

/**
 * XML data for building the worksheet.
 */
public StringBuilder data = new StringBuilder();

/**
 * The name of this worksheet's entry in the XLSX file.
 */
public String zipEntryName;

/**
 * Tracks the last row written to the spreadsheet.
 */
// xslx rows start at 1
// Changed lastRow to init at 0 after using startRow() for headers.
public int lastRow = 0;

/**
 * Tracks the last cell written to the spreadsheet.
 */
public int lastCell = 0;

/**
 * Stores any styles that have been generated using XSSF.
 */
public HashMap<String, XSSFCellStyle> styles = new HashMap<String, XSSFCellStyle>();

/**
 * Tracks any merged cells so that they can be appended to the worksheet XML.
 */
public List<String> merged = new ArrayList<String>();

private boolean inRow = false;

private XSSFSheet myWorksheet = null;

public void setPOIWorksheet(XSSFSheet sheet){
    myWorksheet = sheet;
    this.zipEntryName = sheet.getPackagePart().getPartName().getName().substring(1);
}
public XSSFSheet getPOIWorksheet(){
    return this.myWorksheet;
}

/**
 * Write the raw XML data of newSheets to the existing XLSX file in workbook.
 * @param workbook The current XSLX file to overwrite data in.
 * @param newSheets A Collection of Worksheet objects containing the XML data to insert into workbook.
 * @param newFile The OutputStream to write the new XLSX file to.
 */
public static void writeWorksheetsToWorkbook(InputStream workbook, Collection<Worksheet> newSheets, OutputStream newFile)
{
    ZipOutputStream zipStream = null;
    try{
        zipStream = new ZipOutputStream(newFile);
        ZipInputStream zip = new ZipInputStream(workbook);
        ZipEntry entry;
        // Copy unaffected entries.
        while((entry = zip.getNextEntry()) != null){
            boolean found = false;
            for(Worksheet ws : newSheets){
                if(entry.getName().equals(ws.zipEntryName)){
                    found = true;
                    break;
                }
            }
            if(!found){
                zipStream.putNextEntry(new ZipEntry(entry.getName()));
                byte[] buffer = new byte[1];
                while((zip.read(buffer, 0, 1)) > -1)
                    zipStream.write(buffer);
            }
        }
        // Insert XML for entries being replaced.
        for(Worksheet ws : newSheets){
            zipStream.putNextEntry(new ZipEntry(ws.zipEntryName));
            byte[] data = ws.data.toString().getBytes();
            zipStream.write(data, 0, data.length);
        }
    }catch(Exception e){
        logger.error("Error creating xlsx", e);
    }finally{
        if(zipStream != null) try{ zipStream.close(); }catch(Exception e){}
        if(newFile != null) try{ newFile.close(); }catch(Exception e){}
    }
}

/**
 * Write the raw XML data of newSheets to the existing XLSX file in workbook.
 * @param workbook The current XSLX file to overwrite data in.
 * @param newSheets A Collection of Worksheet objects containing the XML data to insert into workbook.
 * @param return A byte[] containing the new workbook.
 */
public static byte[] writeWorksheetsToWorkbook(InputStream workbook, Collection<Worksheet> newSheets){
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    writeWorksheetsToWorkbook(workbook, newSheets, bout);
    return bout.toByteArray();
}

public Worksheet setWorksheetName(XSSFSheet xssfWS){
    zipEntryName = xssfWS.getPackagePart().getPartName().getName().substring(1);

    return this;
}

/**
 * Write all of the XML used for starting the worksheet.
 */
public Worksheet startWorksheet(){
    data.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    data.append("<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" ");
    data.append("xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" ");
    data.append("xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" >\n");
    data.append("<sheetData>\n");

    return this;
}

/**
 * Write the XML for closing the worksheet, including merged cell data.
 */
public Worksheet closeWorksheet(){
    data.append("</sheetData>\n");

    if(merged.size() > 0){
        data.append("<mergeCells count=\"");
        data.append(merged.size());
        data.append("\">");
        for(String cells : merged){
            data.append("<mergeCell ref=\"");
            data.append(cells);
            data.append("\"/>");
        }
        data.append("</mergeCells>\n");
    }       
    data.append("</worksheet>\n");

    return this;
}

/**
 * Method for adding a new row to an Excel file.  This was added as part of Period Compliance Report because
 * we're not iterating over data like previous reports.
 * 
 * This will automatically close the previous row if left open.
 */
public Worksheet startRow(){
    lastCell = 0;
    if(inRow)
        endRow();

    lastRow++;
    data.append("<row r=\""+lastRow+"\">");
    inRow = true;

    return this;
}

/**
 * Method for closing a row in an Excel file.
 */
public Worksheet endRow(){
    data.append("</row>\n");
    inRow = false;

    return this;
}

/**
 * Method for adding Date data to an Excel file cell.
 * @param value The data to be added to the cell.
 */
public Worksheet addDateCell(String value){
    return addTextCell(value, null);
}
/**
 * Method for adding Date data to an Excel file cell.
 * @param value The data to be added to the cell.
 * @param formatting Any style formatting to be used.
 */
public Worksheet addDateCell(String value, String formatting){
    return addTextCell(value, formatting);
}

/**
 * Method for adding String data to an Excel file cell. This was added as part of Period Compliance Report because
 * we're not iterating over data like previous reports.
 * @param value The data to be added to the cell.
 */
public Worksheet addTextCell(String value){
    return addTextCell(value, null);
}
/**
 * Method for adding String data to an Excel file cell.
 * @param value The data to be added to the cell.
 * @param formatting Any style formatting used on the cell.
 */
public Worksheet addTextCell(String value, String formatting){
    return addCell(StringEscapeUtils.escapeXml(value), formatting, false, 0);
}
/**
 * Method for adding String data to an Excel file cell.
 * @param value The data to be added to the cell.
 * @param formatting Any style formatting used on the cell.
 * @param mergeRight The number of cells to the right of this one that should be merged.
 */
public Worksheet addMergedTextCell(String value, String formatting, int mergeRight){
    return addCell(StringEscapeUtils.escapeXml(value), formatting, false, mergeRight);
}


/**
 * Method for adding numerical data to an Excel file cell.
 * @param value The data to be added to the cell.
 */
public Worksheet addNumberCell(String value){
    return addNumberCell(value,  null);
}
/**
 * Method for adding numerical data to an Excel file cell.
 * @param value The data to be added to the cell.
 */
public Worksheet addNumberCell(Number value){
    return addNumberCell(value.toString(), null);
}
/**
 * Method for adding numerical data to an Excel file cell.
 * @param value The data to be added to the cell.
 * @param formatting Any style formatting used on the cell.
 */
public Worksheet addNumberCell(String value, String formatting){
    return addCell(value, formatting, true, 0);
}
/**
 * Method for adding numerical data to an Excel file cell.
 * @param value The data to be added to the cell.
 * @param formatting Any style formatting used on the cell.
 */
public Worksheet addNumberCell(Number value, String formatting){
    return addCell(value.toString(), formatting, true, 0);
}
/**
 * Method for adding numerical data to an Excel file cell.
 * @param value The data to be added to the cell.
 * @param formatting Any style formatting used on the cell.
 * @param mergeRight The number of cells to the right of this one that should be merged.
 */
public Worksheet addMergedNumberCell(String value, String formatting, int mergeRight){
    return addCell(value, formatting, true, mergeRight);
}
/**
 * Method for adding numerical data to an Excel file cell.
 * @param value The data to be added to the cell.
 * @param formatting Any style formatting used on the cell.
 * @param mergeRight The number of cells to the right of this one that should be merged.
 */
public Worksheet addMergedNumberCell(Number value, String formatting, int mergeRight){
    return addCell(value.toString(), formatting, true, mergeRight);
}

/**
 * Method for adding data to an Excel file cell.
 * @param value The data to be added to the cell.
 * @param element The cell location on the table row.
 * @param formatting The formatting style to use.
 * @param mergeRight The number of cells that should be merged to the right.
 * @return This Worksheet.
 */
private Worksheet addCell(String value, String formatting, boolean number, int mergeRight){
    String ref = addCell(value, formatting, number);

    if(mergeRight > 0){
        String right = null;
        for(int i = 1; i <= mergeRight; i++)
            right = addCell("", formatting, false);
        merged.add(ref+":"+right);
    }

    return this;
}
/**
 * Method for adding data to an Excel file cell.
 * @param value The data to be added to the cell.
 * @param element The cell location on the table row.
 * @param formatting The formatting style to use.
 * @return A String with the new cell's location.
 */
private String addCell(String value, String formatting, boolean number){
    String ref = new CellReference(lastRow-1,lastCell).formatAsString();
    data.append("<c ");
    if(formatting != null && styles.containsKey(formatting)){
        XSSFCellStyle style = styles.get(formatting);
        data.append("s=\"");
        data.append(style.getIndex());
        data.append("\" ");
    }else if(formatting != null)
        logger.debug("could not find style "+formatting);
    data.append("r=\"");
    data.append(ref);
    data.append((number) ? "\">" : "\" t=\"inlineStr\">");
    /*if(formatting == null) data.append((number) ? "\">" : "\" t=\"inlineStr\">");
    else{
        data.append("\" t=\"");
        data.append(formatting);
        data.append("\">");
    }*/
    data.append((number) ? "<v>" : "<is><t>");
    data.append(value);
    data.append((number) ? "</v>" : "</t></is>");
    data.append("</c>");
    lastCell++;

    return ref;
}

/**
 * Adds a bunch of cells to a row quickly.
 * @param fields The fields to be added.
 */
public Worksheet quickAdd(String... fields){
    if(!inRow)
        startRow();
    for(int i = 0; i < fields.length; i++)
        addTextCell(fields[i]);

    return this;
}
}