package test; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import java.util.Collection; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @RunWith(Parameterized.class) public class TestShiftBug { /** * Bug: sheet.shiftRows method produces and unreadable file if third argument is in module greater than the number of rows being shifted and rows being shifted do not include last row. * Result: testShiftResult_1_1, testShiftResult_2_1, testShiftResult_2_2 are readable, testShiftResult_1_2, testShiftResult_1_3, testShiftResult_2_3 are unreadable. * OS: Windows 7. * Excel: Microsoft Excel. */ @Test public void testShift() { try ( FileOutputStream fileOut = new FileOutputStream("testShiftResult_"+rowsToShift+"_"+positionsToShift+".xlsx"); Workbook wb = new XSSFWorkbook(); ){ Sheet sh = wb.createSheet(); sh.createRow(0).createCell(0).setCellValue("a"); sh.createRow(1).createCell(0).setCellValue("b"); sh.createRow(2).createCell(0).setCellValue("c"); sh.shiftRows(sh.getFirstRowNum(), sh.getFirstRowNum()+rowsToShift-1, positionsToShift); wb.write(fileOut); } catch (IOException | EncryptedDocumentException e) { e.printStackTrace(); } } @Parameterized.Parameters public static Collection testShiftParameters() { return Arrays.asList(new Integer[][] { { 1, 1 }, { 1, 2 }, { 1, 3 }, { 2, 1 }, { 2, 2 }, { 2, 3 } }); } private int rowsToShift; private int positionsToShift; public TestShiftBug(int rowsToShift, int positionsToShift) { this.rowsToShift = rowsToShift; this.positionsToShift = positionsToShift; } }