poi 4 Сдвиг строк, что дает ошибку, но работает для poi 3 - PullRequest
0 голосов
/ 22 октября 2018

Привет! Я недавно перешел на poi 4 из poi 3. Я перемещаю некоторые строки в таблице и вставляю новые строки.В моем листе под таблицей есть несколько таблиц, в которые я вставляю строки и обновляю ссылки на эти таблицы.Этот код работал для poi 3. Недавно я перешел на poi 4, и этот код начал повреждать файл.Я подозреваю, что источником проблем является справочная часть обновления.Пожалуйста, дайте мне знать, что мне не хватает.

sheet.shiftRows(firstEmptyRow, sheet.getLastRowNum(), iRowsToBeInserted, true, true);

    Row destrow = null;
    int finalRow = iRowsToBeInserted + firstEmptyRow - 1;

    for (int i = firstEmptyRow; i <= finalRow; i++) {
        destrow = sheet.getRow(i);
        if (destrow == null) {
            System.out.println("row at " + i + " is null : creating new");
            destrow = sheet.createRow(i);
        }
        for (int j = startColumn; j <= endColumn; j++) {
            Cell oldCell = sourceRow.getCell(j);
            Cell NewCell = destrow.createCell(j);
            if (oldCell == null) {
                System.out.println("source cell is null!!!");
                oldCell = sourceRow.createCell(j);
            } else {
                System.out.println("old cell value " + parseCell(oldCell));
            }
            CellStyle newCellStyle = iWorkbook.createCellStyle();

            newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
            if (oldCell.getCellStyle() != null && oldCell.getCellStyle().getFillBackgroundColorColor() != null
                    && oldCell.getCellStyle().getFillForegroundColorColor() != null) {
                System.out.println("fill background color " + oldCell.getCellStyle().getFillBackgroundColor()
                        + " fillb color color " + oldCell.getCellStyle().getFillBackgroundColorColor().toString()
                        + " fill foreground color " + oldCell.getCellStyle().getFillForegroundColor()
                        + " fillc color color " + oldCell.getCellStyle().getFillForegroundColorColor().toString());
            } else {
                System.out.println("either old cell cellstyle is null or its fill colors are null");
            }
            NewCell.setCellStyle(newCellStyle);
            // If there is a cell comment, copy
            if (oldCell.getCellComment() != null) {
                NewCell.setCellComment(oldCell.getCellComment());
            }

            // If there is a cell hyperlink, copy
            if (oldCell.getHyperlink() != null) {
                NewCell.setHyperlink(oldCell.getHyperlink());
            }
            NewCell.setCellType(oldCell.getCellType());
        }
    }
    crEnd = new CellReference(crEnd.getSheetName(), finalRow, endColumn, crEnd.isRowAbsolute(), crEnd.isColAbsolute());
    final AreaReference newArea = new AreaReference(crStart, crEnd, iWorkbook.getSpreadsheetVersion());
    iTable.setArea(newArea);
    iTable.updateReferences();
    for (XSSFTable t : sheet.getTables()) {
        t.updateReferences();
        crStart = t.getStartCellReference();
        crEnd = t.getEndCellReference();
        AreaReference tableArea;
        System.out.println("Co-ordinates of table: " + t.getName() + " start x:y " + crStart.getRow() + ":"
                + crStart.getCol() + " end x:y " + crEnd.getRow() + ":" + crEnd.getCol());
        if (t.getName().equals(iTable.getName()) || crEnd.getRow() < newArea.getFirstCell().getRow()) {
            System.out.println("table is/before expanded table - continuing");
            continue;
        } else {
            System.out.println("table needs to be updated");
            CellReference crStartNew = new CellReference(crStart.getSheetName(), crStart.getRow() + iRowsToBeInserted, crStart.getCol(),crStart.isRowAbsolute(), crStart.isColAbsolute());
            CellReference crEndNew = new CellReference(crEnd.getSheetName(), crEnd.getRow() + iRowsToBeInserted, crEnd.getCol(), crEnd.isRowAbsolute(), crEnd.isColAbsolute());
            tableArea = new AreaReference(crStartNew, crEndNew, iWorkbook.getSpreadsheetVersion());
            AreaReference  arefArea = t.getArea();
            System.out.println(arefArea.toString());
            System.out.println(tableArea.toString());
            t.setArea(tableArea);
            t.updateReferences();
        }
    }

1 Ответ

0 голосов
/ 21 июня 2019

Это «известный» баг.К сожалению, это не решено.Для решения этой проблемы существует обходной путь

Наиболее интересный снимок:

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);
                }
            }
        }
...