1

EDIT to not make you read everything :

Sorry for bothering, I forgot all this was in a try catch clause and I never did anything in the catch part. Moving some code around made me realize this. Because of that I realized that the issue was due to the array allocated. I got :

Tried to allocate an array of length 112,077,441, but the maximum length for this record type is 100,000,000.

Then adding IOUtils.setByteArrayMaxOverride(113000000); to change the maximum length allocated resulted in this :

Zip bomb detected! The file would exceed the max. ratio of compressed file size to the size of the expanded data.
This may indicate that the file is used to inflate memory usage and thus could pose a security risk.
You can adjust this limit via ZipSecureFile.setMinInflateRatio() if you need to work with files which exceed this limit.
Uncompressed size: 4085106, Raw/compressed size: 40851, ratio: 0.010000
Limits: MIN_INFLATE_RATIO: 0.010000, Entry: xl/styles.xml

Which was fixed by ZipSecureFile.setMinInflateRatio(0.001);

Sorry about all that :/

Original post

I am trying to read an xlsx file with the apache POI library to produce multiple output files but the execution seems to stop on the instantiation of the XSSFWorkbook object.

Here is my code

File testFile = new File("C:\\Users\\Public\\test.xlsx");

System.out.println(testFile.getName()); //output: test.xlsx
System.out.println(testFile.getAbsolutePath()); //output: C:\Users\Public\test.xlsx
System.out.println(testFile.canRead()); //output: true

FileInputStream fis = new FileInputStream(testFile); 

System.out.println("Load xlsx"); // output: Load xlsx
XSSFWorkbook mReadingXlSession = new XSSFWorkbook(fis); //never go past this point

System.out.println("start loading EIS"); // never reach this

I have tested creating an empty instance and it worked but I need to read the file.

I have tried with Workbook wb = WorkbookFactory.create(fis) instead but it didn't work.

I tried replacing the FileInputStream with OPCPackage pkg = OPCPackage.open(testFile) but it didn't change anything.

The file I am trying to read is about 18MB for around 8500 lines split into 12 sheets and less than 20 columns per sheet. I thought maybe the size was making the instantiation long but I increased the minimum heap size to 256MB and waited for more than 20 minutes but nothing changed.

I tried with the method shown here https://poi.apache.org/components/spreadsheet/how-to.html#xssf_sax_api and it seems I have results but I don't know how to use the values extracted from the file this way. Depending on the values I will need to do things a bit differently and using the Workbook and Sheet methods are really helpful for that.

I am using the last version of poi-ooxml (5.2.2), IDE is IntelliJ and I am using Java 11.

EDIT :

I changed the code a little bit as I was using javaFX for a UI part and thought that for some weird reason it might have an impact. Now there is only the steps required for the instantiation of a XSSFWorkbook and System.out.println(). After using the logger at level "all" and trying multiple ways of creation of XSSFWorkbook, I get this when instantiating my object :

DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/_rels/workbook.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/worksheets/_rels/sheet1.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/worksheets/_rels/sheet2.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/worksheets/_rels/sheet3.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/worksheets/_rels/sheet4.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/worksheets/_rels/sheet5.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/worksheets/_rels/sheet6.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/worksheets/_rels/sheet7.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/worksheets/_rels/sheet8.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/worksheets/_rels/sheet9.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/worksheets/_rels/sheet10.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/worksheets/_rels/sheet11.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /xl/worksheets/_rels/sheet12.xml.rels
DEBUG PackageRelationshipCollection:309 - Parsing relationship: /_rels/.rels
TRACE XBeanDebug:196 - Loading type system org.apache.poi.schemas.ooxml.system.ooxml
TRACE XBeanDebug:196 - Loading type system org.apache.xmlbeans.metadata.system.sXMLCONFIG
TRACE XBeanDebug:196 - Loading type system org.apache.xmlbeans.metadata.system.sXMLLANG
TRACE XBeanDebug:196 - Loading type system org.apache.xmlbeans.metadata.system.sXMLSCHEMA
TRACE XBeanDebug:196 - Loading type system org.apache.xmlbeans.metadata.system.sXMLTOOLS
TRACE XBeanDebug:231 - Reading unresolved handles for type system org.apache.xmlbeans.metadata.system.sXMLTOOLS
TRACE XBeanDebug:206 - Finished loading type system org.apache.xmlbeans.metadata.system.sXMLTOOLS
TRACE XBeanDebug:231 - Reading unresolved handles for type system org.apache.xmlbeans.metadata.system.sXMLSCHEMA
TRACE XBeanDebug:206 - Finished loading type system org.apache.xmlbeans.metadata.system.sXMLSCHEMA
TRACE XBeanDebug:231 - Reading unresolved handles for type system org.apache.xmlbeans.metadata.system.sXMLLANG
TRACE XBeanDebug:206 - Finished loading type system org.apache.xmlbeans.metadata.system.sXMLLANG
TRACE XBeanDebug:231 - Reading unresolved handles for type system org.apache.xmlbeans.metadata.system.sXMLCONFIG
TRACE XBeanDebug:206 - Finished loading type system org.apache.xmlbeans.metadata.system.sXMLCONFIG
TRACE XBeanDebug:231 - Reading unresolved handles for type system org.apache.poi.schemas.ooxml.system.ooxml
TRACE XBeanDebug:206 - Finished loading type system org.apache.poi.schemas.ooxml.system.ooxml
TRACE XBeanDebug:937 - Resolving type for handle themefd26doctype
TRACE XBeanDebug:937 - Resolving type for handle stylesheet5d8bdoctype

Process finished with exit code 0

After having that, the execution stops. Which is strange as I have System.out.println coming after the instantiation and I don't get any error.

RGloum
  • 11
  • 3
  • 1
    "execution seems to stop on the instantiation of the XSSFWorkbook object": What I would do first is to set the logger level to ALL. Then have a look in the logs. What is logged there? – Axel Richter May 30 '22 at 05:20
  • what is the exact error? out of memory?? – vh1ne Jun 14 '22 at 18:32
  • Thanks for this question, it saved me possibly a lot of time. – Ravi Wallau Apr 05 '23 at 19:42
  • Suggestion: It looks as if you have a solution, which is great. But instead of adding those details to the question, you are always welcome to [write a proper answer](https://stackoverflow.com/help/self-answer). (And you can delete those sections from the question.) – andrewJames Apr 05 '23 at 19:48

1 Answers1

0

Try to use InputStream instead of FileInputStream, such as:

InputStream is = Files.newInputStream(path)

Workbook wb = new XSSFWorkbook(is)

sergiob
  • 1
  • 1