I create three groups being the root node. I collapse each group as well using setRowGroupCollapsed. However, when I expand the first node on the Excel File generated, all the other nodes are also expanded. To prevent this I apply this patch in the class org.apache.poi.hssf.record.aggregates.RowRecordsAggregate, method collapseRow : public void collapseRow( int rowNumber ) { // Find the start of the group. int startRow = findStartOfRowOutlineGroup( rowNumber ); RowRecord rowRecord = getRow( startRow ); //I add this line rowRecord.setColapsed(true); // Hide all the columns until the end of the group int lastRow = writeHidden( rowRecord, startRow, true ); // Write collapse field if (getRow(lastRow + 1) != null) { getRow(lastRow + 1).setColapsed( true ); } else { RowRecord row = createRow( lastRow + 1); row.setColapsed( true ); insertRow( row ); } }
Created attachment 23816 [details] The classe RowRecordsAggregate
Your comment was: Thanks for the bug-report and suggested fix. So your proposed change was to add the line "rowRecord.setColapsed(true);" in collapseRow(int): public void collapseRow(int rowNumber) { // Find the start of the group. int startRow = findStartOfRowOutlineGroup(rowNumber); RowRecord rowRecord = getRow(startRow); + rowRecord.setColapsed(true); // Hide all the columns until the end of the group int nextRowIx = writeHidden(rowRecord, startRow); Unfortunately this is NQR. I think we need to mark the previous row as 'collapsed', not the first row. For example, with the following POI calls: sheet.setRowSumsBelow(false); sheet.groupRow(3, 5); sheet.setRowGroupCollapsed(3, true); We should get the following 'outline levels' and flags in the rows: 2 ol=0 collapsed 3 ol=1 hidden 4 ol=1 hidden 5 ol=1 hidden Currently POI does this: 3 ol=1 hidden 4 ol=1 hidden 5 ol=1 hidden 6 ol=0 collapsed (Which is actually correct only if sheet.rowSumsBelow == true) I've done some experimenting with how Excel collapses rows and it appears that the row with the 'collapsed' flag should appear above *or* below the group according to the state of the 'rowSumsBelow' flag on the sheet. POI assumes the 'collapsed' flag belongs on the group subsequent row which is wrong if rowSumsBelow is false. POI should also be changed so that the 'rowSumsBelow' property in HSSFSheet gets coordinated with the underlying RowRecordsAggregate. The RowRecordsAggregate needs to know the value of this flag in order to find the right row to update the 'collapsed' flag on. Furthermore, when 'rowSumsBelow' changes in the sheet, the RowRecordsAggregate will also need to transfer the 'collapsed' flags of each row group accordingly. Note - Excel's overall model for collapsing row groups works OK as long as every group has its own dedicated summary row (above or below). It's not too hard to create groups that have no summary row, and Excel behaves a little weirdly in these cases. POI probably shouldn't bother supporting such anomalous use cases, and that should be mentioned in the javadoc.
Thank you for your comments. But I think I have not good explain my problem. In my example, I have one root group, and two sub-group. And when I expand the root group, the sub-group are also expanded. I attach a file example. My patch fixes the problem. In my example the 'rowSumsBelow' property is false. I hope you understand what I mean.
Created attachment 23824 [details] test case
This patch is now quite outdated and thus it is very hard to see what the actual changes were here because a full file was provided instead of a clean patch with only the relevant changes. Setting this to NEEDINFO unless someone comes up with a refreshed patch that applies cleanly to the current version again.