Bug 52348

Summary: XSSFWorkbook#createCellStyle() fails with ArrayIndexOutOfBoundsException
Product: POI Reporter: nestor.urquiza
Component: XSSFAssignee: POI Developers List <dev>
Severity: normal    
Priority: P2    
Version: 3.8-dev   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: 5 files showing the issue
two screenshots showing how the second style of the merged sheet gets messed up

Description nestor.urquiza 2011-12-16 20:42:05 UTC
Created attachment 28079 [details]
5 files showing the issue

This is a bug resulting from a discussion on the mailing list http://apache-poi.1045710.n5.nabble.com/XSSFWorkbook-createCellStyle-fails-with-ArrayIndexOutOfBoundsException-td5033683.html

I am attaching 5 files (two input xlsx files with the output xlsx you can obtain yourself if you run the JUnit class that uses the Util class). One is a util class that should allow me to merge two XLSX books and the other a JUnit test that cannot run and creates an output file where the style for the second sheet is missing.

Look for the below lines and uncomment the code in the Util class then run the test again. The Exception will be thrown:

//BUG: The below produces:
                    java.lang.ArrayIndexOutOfBoundsException: -1
                    at java.util.ArrayList.get(ArrayList.java:324)
                    at org.apache.poi.xssf.model.StylesTable.getCellStyleXfAt(StylesTable.java:305)
                    at org.apache.poi.xssf.usermodel.XSSFCellStyle.<init>(XSSFCellStyle.java:79)
                    at org.apache.poi.xssf.model.StylesTable.createCellStyle(StylesTable.java:521)
                    at org.apache.poi.xssf.usermodel.XSSFWorkbook.createCellStyle(XSSFWorkbook.java:443)
                    //newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
                    //styleMap.put(stHashCode, newCellStyle);

Comment 1 Nick Burch 2011-12-17 10:45:44 UTC
Are you able to unpack one of the problematic files (.xlsx files are a zip of xml files), and see if there's anything obviously different in the styles file between one that works and one that doesn't?

For a problem file, does adding or removing styles in excel fix things?
Comment 2 Yegor Kozlov 2011-12-19 08:47:42 UTC
I confirmed the problem in trunk. 

The simplest failing test case is:

XSSFWorkbook targetWorkbook = new XSSFWorkbook(new FileInputStream("balance.xlsx"));
XSSFCellStyle style = targetWorkbook.createCellStyle();

Somehow appending a new cell style to balance.xlsx results in exception. 

Comment 3 Yegor Kozlov 2011-12-19 09:06:38 UTC
Fixed in r1220659, junit added

Comment 4 nestor.urquiza 2012-01-03 18:44:16 UTC
Created attachment 28118 [details]
two screenshots showing how the second style of the merged sheet gets messed up

Hi Yegor,

First, Happy and healthy 2012! 

Thanks for looking at this issue. I can confirm the exception is not longer generated however the style for the second document is not respected after the merge. If you run my JUnit test you can confirm so but in any case I am attaching the screenshots for you to have a quick look at what is currently happening.