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

Pra*_*kar 0 java excel apache-poi

我正在尝试使用从数据库获取的数据创建多个 Excel 工作表。每个 Excel 工作表包含 60 多个列和大约 50k 条记录(可能有所不同)。问题是系统花费了很多时间(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
Run Code Online (Sandbox Code Playgroud)

已经提到了几个SO问题,但无法找出任何有用的东西。想知道我是否应该尝试什么才能克服这个问题。

Rai*_*ner 6

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

\n

确保您使用 apache.poi API的流式 api

\n

org.apache.poi.xssf.streaming

\n

这是apache的demo

\n

大网格演示

\n

更新

\n

正如我链接到的演示中所述,您可能应该使用新的 SXSSF 用户模型(如果我没记错的话,我使用过该模型),因为它为您处理所有流媒体内容;-)

\n

SXSSF(流用户模型 API)

\n