This is the exception that occurs when i try to group columns on sheet: java.lang.ArrayIndexOutOfBoundsException: -2 at java.util.ArrayList.get(ArrayList.java:323) at org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate.getColInfo(ColumnInfoRecordsAggregate.java:151) at org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate.collapseColInfoRecords(ColumnInfoRecordsAggregate.java:448) at org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate.groupColumnRange(ColumnInfoRecordsAggregate.java:492) at org.apache.poi.hssf.model.Sheet.groupColumnRange(Sheet.java:1194) at org.apache.poi.hssf.usermodel.HSSFSheet.groupColumn(HSSFSheet.java:1596) example code: final HSSFWorkbook wb = new HSSFWorkbook(); final HSSFSheet sheet = wb.createSheet("test"); sheet.groupColumn((short)7, (short)9); sheet.groupColumn((short)4, (short)12); sheet.groupColumn((short)1, (short)15); Tested on current poi version (poi-3.2-alpha1-20080817)
A simple patch is to comment out the performance tuning with fromIdx in ColumnInfoRecordsAggregate.groupColumnRange(short fromColumn, short toColumn, boolean indent). It does not work because the column records are NOT ordered by column no: // taken from 3.1 FINAL org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate: public void groupColumnRange(short fromColumn, short toColumn, boolean indent) { ... // OD bug fix: records NOT ordered by column no // fromIdx = columnIdx - 1; // subtract 1 just in case this column is collapsed later. ... }
fixed in svn 694534. As you pointed out, the ColumnInfoRecords were not being kept in proper order. Rather than disable the optimisation, I fixed the logic to keep the records sorted. There were a few other methods besides groupColumnRange() that relied on proper ordering. Junit added for groupColumnRange() and collapseColumn()/expandColumn()