diff --git a/poi/src/main/java/org/apache/poi/hssf/model/InternalWorkbook.java b/poi/src/main/java/org/apache/poi/hssf/model/InternalWorkbook.java index a60fec9fd6..a536d74dd0 100644 --- a/poi/src/main/java/org/apache/poi/hssf/model/InternalWorkbook.java +++ b/poi/src/main/java/org/apache/poi/hssf/model/InternalWorkbook.java @@ -1071,13 +1071,16 @@ public final class InternalWorkbook { * * Include in it ant code that modifies the workbook record stream and affects its size. */ - public void preSerialize(){ + public void preSerialize() { // Ensure we have enough tab IDs // Can be a few short if new sheets were added - if(records.getTabpos() > 0) { - TabIdRecord tir = ( TabIdRecord ) records.get(records.getTabpos()); - if(tir.getTabIdSize() < boundsheets.size()) { - fixTabIdRecord(); + if (records.getTabpos() > 0) { + Record rec = records.get(records.getTabpos()); + if (rec instanceof TabIdRecord) { + TabIdRecord tir = ( TabIdRecord ) rec; + if(tir.getTabIdSize() < boundsheets.size()) { + fixTabIdRecord(); + } } } } diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFSheetShiftRows.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFSheetShiftRows.java index 96273fe965..4a0ab65b73 100644 --- a/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFSheetShiftRows.java +++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/TestHSSFSheetShiftRows.java @@ -18,11 +18,32 @@ package org.apache.poi.hssf.usermodel; import org.apache.poi.hssf.HSSFITestDataProvider; +import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.ss.usermodel.BaseTestSheetShiftRows; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertNotNull; final class TestHSSFSheetShiftRows extends BaseTestSheetShiftRows { public TestHSSFSheetShiftRows() { super(HSSFITestDataProvider.instance); } + + @Test + public void testBug69021() throws IOException { + try (HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("bug69021.xls")) { + Sheet sheet = workbook.getSheetAt(0); + int rowIndex = 2; + sheet.shiftRows(rowIndex, sheet.getLastRowNum(), 1); + Row row = sheet.createRow(rowIndex); + row.createCell(0).setCellValue("switch"); + HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(workbook); + assertNotNull(wbBack); + } + } } diff --git a/test-data/spreadsheet/bug69021.xls b/test-data/spreadsheet/bug69021.xls new file mode 100644 index 0000000000..992cce6abc Binary files /dev/null and b/test-data/spreadsheet/bug69021.xls differ