I'm using Apache POI (4.1.1) to modify an existing workbook. The workbook contains four sheets. I'm using the second sheet as a template by cloning it, then writing data to the new cloned sheets. It's cloned around six times or so. At the end of this process I remove the template sheet, then I try to set a sheet order to all the sheets: the existing sheets (minus the template) and then the sheets I cloned. I'm using XSSFWorkbook::setSheetOrder to do this, and I see it change the index in debug, but when my Excel file actually writes out, the sheets are not in order.
Here is the relevant code:
// wb is an XSSFWorkbook
wb.removeSheetAt(wb.getSheetIndex("Original Sheet 2")); // Remove the template sheet
wb.setSheetOrder("Original Sheet 1", 0);
for (ReportPresenter presenter : dataMap.values()) {
String sheetTitle = makeSheetTitleFromPresenter(presenter);
int oldIndex = wb.getSheetIndex(wb.getSheet(sheetTitle));
wb.setSheetOrder(sheetTitle, presenter.getAnalysisNumber());
int newIndex = wb.getSheetIndex(wb.getSheet(sheetTitle));
System.out.println(oldIndex + " -> " + newIndex);
}
wb.setSheetOrder("Original Sheet 3", dataMap.size() + 1);
wb.setSheetOrder("Original Sheet 4", dataMap.size() + 2);
I included the debug println above to show here how I know that the index is actually being changed to the order I want with the setSheetOrder method. However, when I open the Excel file that is generated by this code, my sheets all exist but they are not in the order I have specified.
I would really appreciate some ideas! Thank you.

