笔记——》记录一个读取excel的GC overhead limit exceeded问题

455 阅读1分钟

报错原因

本地有一个大容量excel,里面有百万行数据,整个excel有二三十兆,通过Workbook读取的时候,报错java.lang.OutOfMemoryError: GC overhead limit exceeded。

fileInputStream = new FileInputStream(new File(filePath));
Workbook workbook = new XSSFWorkbook(fileInputStream);

1696830378253.png

解决方法

通过 easyexcel工具来读取

在pom文件中添加依赖

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.0</version>
        </dependency>
//listMap 即为数据集合
List<Map<String, String>> listMap = EasyExcel.read(new File(filePath)).sheet(0).doReadSync();

image.png 完事儿...

踩坑记录

网上还有一种方法是通过xlsx-streamer来解决的。

InputStream is = new FileInputStream(new File("/path/to/workbook.xlsx"));
Workbook workbook = StreamingReader.builder()
        .rowCacheSize(100)    // number of rows to keep in memory (defaults to 10)
        .bufferSize(4096)     // buffer size to use when reading InputStream to file (defaults to 1024)
        .open(is);           

这种方法需要修改本地poi的版本号,不然会报错

java.lang.ClassNotFoundException: org.apache.poi.util.StaxHelper

所以相较之下选择了上面easyexcel的这种方式