0
votes

I'm facing OutOfMemoryError when I try to read, format and write a line from a xlsx file with 23MB. I have a requirement to format only the header of the file.

I've tried with InputStream, File and OPCPackage:

1)
InputStream inp = new FileInputStream(path);
XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(inp);

2)
InputStream inp = new FileInputStream(path);
File file = new File(path);
Files.copy(inp, file.toPath(), StandardCopyOption.REPLACE_EXISTING);            
XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(file);

3)
OPCPackage pkg = OPCPackage.open(path);
XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(pkg);

No success at all.

I have seen some examples using XSSFReader, SXSSFWorkbook, but it couldn't resolve my case because I need to overwrite the line, so I think I really need a Workbook.

Does anybody have implemented a read and write operation using Apache POI with a large xlsx file?

PS: In case the solution is to generate another file I need the format of the original file because the content is already formatted.

1

1 Answers

0
votes

I updated POI version from 3.17 from "Sep, 2017" to 5.0.0 from "Jan, 2021". And also commons-compress from 1.4.1 to 1.20.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.20</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.0.0</version>
</dependency>

Now I can open the file and make changes.