POI 生成百万行Excel防止OOM

最近用XSSFWorkbook做Excel导出时遇到了一个问题:当数据达到几万行会出现java.lang.OutOfMemoryError: GC overhead limit exceeded错误。

解决办法:

SXSSF(包:org.apache.poi.xssf.streaming)是XSSF的API兼容流式扩展,用于在必须生成非常大的电子表格时使用,并且堆空间有限。SXSSF通过限制对滑动窗口内行的访问来实现其低内存占用,而XSSF允许访问文档中的所有行。不再在窗口中的旧行变得不可访问,因为它们被写入磁盘。

详细介绍请查看:http://poi.apache.org/components/spreadsheet/how-to.html#sxssf

测试类:

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * SXSSFWorkbook测试
 *
 * @author 王晓安
 */
public class SXSSFWorkbookTest {

    private static SXSSFWorkbook getWorkbook(List<String> title, List<? extends List<?>> data) {
        SXSSFWorkbook workbook = new SXSSFWorkbook();
        // 添加一个sheet
        final Sheet sheet = workbook.createSheet();
        // 构建title
        final Row titleRow = sheet.createRow(0);
        for (int i = 0; i < title.size(); i++) {
            final Cell titleRowCell = titleRow.createCell(i);
            titleRowCell.setCellValue(title.get(i));
        }
        // 填充数据
        for (int i = 0; i < data.size(); i++) {
            final Row row = sheet.createRow(i + 1);
            final List<?> dataRow = data.get(i);
            for (int j = 0; j < dataRow.size(); j++) {
                final Cell cell = row.createCell(j);
                final Object value = dataRow.get(j);
                cell.setCellValue(value == null ? "" : String.valueOf(value));
            }
        }
        return workbook;
    }

    public static void main(String[] args) {
        int col = 10;
        int row = 100_0000;
        final List<String> title = IntStream.rangeClosed(1, col)
                .mapToObj(value -> "第" + value + "列")
                .collect(Collectors.toList());

        final List<List<Double>> data = IntStream.range(0, row)
                .mapToObj(value ->
                        IntStream.range(0, col)
                                .mapToObj(ignore -> Math.random())
                                .collect(Collectors.toList())
                )
                .collect(Collectors.toList());

        final LocalDateTime start = LocalDateTime.now();
        final SXSSFWorkbook workbook = getWorkbook(title, data);
        try (OutputStream outputStream = new FileOutputStream("/data/temp/测试.xlsx")) {
            workbook.write(outputStream);
            // 丢弃在磁盘上备份此工作簿的临时文件
            workbook.dispose();
        } catch (IOException e) {
            e.printStackTrace();
        }
        final LocalDateTime end = LocalDateTime.now();
        final Duration duration = Duration.between(start, end);
        System.out.println("生成Excel花费时间:" + duration);
    }
}

生成一百万行的Excel时间大约32秒:
POI 生成百万行Excel防止OOM_第1张图片

生成的Excel大小如下:
生成的Excel大小

算上标题和数据共一百万零一行:
算上标题和数据共一百万零一行

你可能感兴趣的:(解决问题)