Bug 57482 - InvalidOperationException occurs when creating XSSFWorkbook with PackageAccess.READ
Summary: InvalidOperationException occurs when creating XSSFWorkbook with PackageAcces...
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: unspecified
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
: 55948 56675 (view as bug list)
Depends on:
Reported: 2015-01-22 08:31 UTC by isaokk3
Modified: 2015-01-22 15:04 UTC (History)
2 users (show)

Numeric value only Excel (xlsx) and Unit test (7.74 KB, application/zip)
2015-01-22 08:31 UTC, isaokk3

Note You need to log in before you can comment on or make changes to this bug.
Description isaokk3 2015-01-22 08:31:25 UTC
Created attachment 32390 [details]
Numeric value only Excel (xlsx) and Unit test

I got the following stack trace when creaing XSSFWorkbook with PackageAccess.READ:

Unstructured_Data_0,0: org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidOperationException: Operation not allowed, document open in read only mode!
    at org.apache.poi.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:370)
    at org.apache.poi.POIXMLDocumentPart.createRelationship(POIXMLDocumentPart.java:330)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:274)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:186)

My code is like below.
   XSSFWorkbook workbook = new XSSFWorkbook(OPCPackage.open(file.getAbsolutePath(), PackageAccess.READ));

It usually works fine; however the above exception occurs when reading some Excel (xlsx) files. I found similar bug reports such as 55948 and 56675, but unfortunately they are not fixed yet.

As a result of my investigation, I recognized this issue occurs when reading an xlsx file that contains only numeric data. In this case there is no shared string table, POI tries to create a new part named "/xl/sharedStrings.xml" and then throws the exception.
It seems a POI logic issue. It should be allowed even if PackageAccess.READ is specified.
Comment 1 Nick Burch 2015-01-22 11:46:14 UTC
Thanks for this

I've slightly re-worked your unit test, and added it in r1653814.

Now to fix it...
Comment 2 Nick Burch 2015-01-22 12:18:06 UTC
Should be fixed as of r1653825 - in read only mode we now create an empty unbound shared strings table or styles table to get through the reading calls which might use them

(Unbound is fine, as writing out isn't supported from a read-only package)

Thanks for your help!
Comment 3 Dominik Stadler 2015-01-22 15:02:34 UTC
*** Bug 56675 has been marked as a duplicate of this bug. ***
Comment 4 Dominik Stadler 2015-01-22 15:04:09 UTC
*** Bug 55948 has been marked as a duplicate of this bug. ***