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

            Bug ID: 62759
           Summary: removeColumn corrupts XSSFTable
           Product: POI
           Version: 4.0.0-FINAL
          Hardware: PC
            Status: NEW
          Severity: major
          Priority: P2
         Component: XSSF
          Assignee: dev@poi.apache.org
          Reporter: dmgau...@uab.edu
  Target Milestone: ---

removeColumn does not update the ref field of ctTable.  Excel reports a
corrupted file when he file is opened, and repairs it by deleting the table. 
Unit test code is presented below.  When bApplyBugFix is false, the table is
corrupted.  When bApplyBugFix is true, the table is not corrupted.

- David Gauntt

    public static void doUnitTest(File file) {
        final XSSFWorkbook workbook = new XSSFWorkbook();
        final XSSFSheet sheet = workbook.createSheet();

        removeColumnUnitTest(sheet);

        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");
        final Desktop desktop = Desktop.getDesktop();
        try {
            desktop.open(file);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }

    }

    private static void removeColumnUnitTest(XSSFSheet sheet) {
        final boolean bApplyBugFix = true;
        final String procName = "removeColumnUnitTest";
        final int NUMCOLS = 3, NUMROWS = 4;

        System.out.println(String.format("\r\n%s: bFixBug=%s", procName,
bApplyBugFix));

        /* Fill a range with data */
        for (int i = 0; i < NUMROWS; i++) {
            XSSFRow row = sheet.createRow(i);
            for (int j = 0; j < NUMCOLS; j++) {
                XSSFCell localXSSFCell = row.createCell(j);
                if (i == 0) {
                    localXSSFCell.setCellValue(String.format("Col%d", j + 1));
                } else {
                    localXSSFCell.setCellValue(String.format("(%d,%d)", i + 1,
j + 1));
                }
            }
        }

        /* Define a single column data range including headers */
        AreaReference my_data_range = new AreaReference(new CellReference(0,
0),
                new CellReference(NUMROWS - 1, NUMCOLS - 1),
SpreadsheetVersion.EXCEL2007);

        /* Create an object of type XSSFTable */
        final XSSFTable my_table = sheet.createTable(my_data_range);
        my_table.setDisplayName(procName);

        /* Apply bug fix for creatTable/createColumn */
        final CTTable ctTable = my_table.getCTTable();
        final List<CTTableColumn> ctTableColumns =
ctTable.getTableColumns().getTableColumnList();
        final long numCols = ctTableColumns.size();
        for (int n = 0; n < numCols; ++n) {
            ctTableColumns.get(n).setId(n + 1);
        }
        /* Remove the last column */
        my_table.removeColumn(NUMCOLS - 1);
        System.out.println(String.format("%s: after removing last column from
table", procName));
        System.out.println(my_table.getCTTable().toString());

        if (bApplyBugFix) {
            my_data_range = new AreaReference(new CellReference(0, 0), new
CellReference(NUMROWS - 1, NUMCOLS - 2),
                    SpreadsheetVersion.EXCEL2007);
            my_table.setArea(my_data_range);
            System.out.println("After applying bug fix");
            System.out.println(my_table.getCTTable().toString());
        }

    }

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

Reply via email to