When using HSSFSheet.shiftRows(), the RowRecordsAggregate seems to get inconsistent with the ValueRecordsAggregate, resulting in the following exception: Exception in thread "main" java.lang.IllegalArgumentException: Specified rowIndex 30 is outside the allowable range (0..30) at org.apache.poi.hssf.record.aggregates.ValueRecordsAggregate.removeAllCellsValuesForRow(ValueRecordsAggregate.java:111) at org.apache.poi.hssf.record.aggregates.RowRecordsAggregate.removeRow(RowRecordsAggregate.java:120) at org.apache.poi.hssf.model.Sheet.addRow(Sheet.java:677) at org.apache.poi.hssf.usermodel.HSSFSheet.addRow(HSSFSheet.java:303) at org.apache.poi.hssf.usermodel.HSSFSheet.createRow(HSSFSheet.java:206) Code to reproduce: HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); sheet.createRow(30); sheet.shiftRows(29, 29, 1, true, true); sheet.createRow(30); The first createRow() succeeds, the second fails. The first call can be left out, leading to the same result. As workaroud is not enough to create the target explicitely before shifting the source row (as the reproducer code does), but you need to create a cell in this row. The following code works: HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); sheet.createRow(30).createCell(0); sheet.shiftRows(29, 29, 1, true, true); sheet.createRow(30);
This works with the latest version of POI, 3.12 and likely was fixed some time ago already.