You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by bu...@apache.org on 2018/09/21 17:04:33 UTC

[Bug 57423] shiftRows() produces a corrupted xlsx file

https://bz.apache.org/bugzilla/show_bug.cgi?id=57423

--- Comment #13 from David Gauntt <dm...@uab.edu> ---
This bug has reappeared with version 4.0.0.  The following code demonstrates
the bug, and demonstrates a workaround.  If bFixBug is set to false, Excel 2011
for Mac indicates that the destination file is corrupted.  If bFixBug is set to
true, the file opens without trouble.

        public static void doUnitTest(File file) {
                final XSSFWorkbook workbook = new XSSFWorkbook();
                final XSSFSheet sheet = workbook.createSheet();
                final boolean bFixBug = false;
                final int numRows = 5;
                final int numCols = 5;

                System.out.println("doUnitTest: starting");

                for (int nRow = 0; nRow < numRows; ++nRow) {
                        final XSSFRow row = sheet.createRow(nRow);
                        for (int nCol = 0; nCol < numCols; ++nCol) {
                                final XSSFCell cell = row.createCell(nCol);
                                cell.setCellType(CellType.STRING);
                                cell.setCellValue(String.format("Row %d col
%d", nRow, nCol));
                        }
                }

                final int nFirstRow = 3;
                final int nLastRow = 4;
                final int nNumToShift = 1;
                final int nFirstDstRow = nFirstRow + nNumToShift;
                final int nLastDstRow = nLastRow + nNumToShift;

                sheet.shiftRows(nFirstRow, nLastRow, nNumToShift);
                if (bFixBug) {
                        for (int nRow = nFirstDstRow; nRow <= nLastDstRow;
++nRow) {
                                final XSSFRow row = sheet.getRow(nRow);
                                if (row != null) {
                                        String msg = "Row[rownum=" +
row.getRowNum()
                                                        + "] contains cell(s)
included in a multi-cell array formula. "
                                                        + "You cannot change
part of an array.";
                                        for (Cell c : row) {
                                                ((XSSFCell)
c).updateCellReferencesForShifting(msg);
                                        }
                                }
                        }

                }

                try (OutputStream fileOut = new FileOutputStream(file)) {
                        workbook.write(fileOut);
                } catch (Exception e) {
                        System.err.println(e.getMessage());
                } finally {
                        try {
                                workbook.close();
                        } catch (IOException e) {
                                System.err.println(e.getMessage());
                        }
                }
                System.out.println("doUnitTest: done");
        }

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org