使用Apache POI插入行,但无法复制样式,最后一行移至未知位置

时间:2019-04-28 11:59:28

标签: java excel apache-poi

我使用的是可在互联网上插入的相同解决方案,

但它似乎并没有复制样式和颜色。

以下是代码段:

private static void copyRow(Workbook workbook, Sheet worksheet, int sourceRowNum, int destinationRowNum) {
    // Get the source / new row
    Row newRow = worksheet.getRow(destinationRowNum);
    Row sourceRow = worksheet.getRow(sourceRowNum);

    // If the row exist in destination, push down all rows by 1 else create a new row
    if (newRow != null) {
        worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
    } else {
        newRow = worksheet.createRow(destinationRowNum);
    }

    // Loop through source columns to add to new row
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        Cell oldCell = sourceRow.getCell(i);
        Cell newCell = newRow.createCell(i);

        // If the old cell is null jump to next cell
        if (oldCell == null) {
            newCell = null;
            continue;
        }

        // Copy style from old cell and apply to new cell
        CellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
        ;
        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());
        }

        // Set the cell data type
        newCell.setCellFormula(null);
        if(oldCell.getCellType().equals(CellType.FORMULA)){
            newCell.setCellFormula(oldCell.getCellFormula());
        } else {
            newCell.setCellType(oldCell.getCellType());
        }

        // Set the cell data value
        switch (oldCell.getCellType()) {
            case BLANK:
                newCell.setCellValue(oldCell.getStringCellValue());
                break;
            case BOOLEAN:
                newCell.setCellValue(oldCell.getBooleanCellValue());
                break;
            case ERROR:
                newCell.setCellErrorValue(oldCell.getErrorCellValue());
                break;
            case FORMULA:
                newCell.setCellFormula(oldCell.getCellFormula());
                break;
            case NUMERIC:
                newCell.setCellValue(oldCell.getNumericCellValue());
                break;
            case STRING:
                newCell.setCellValue(oldCell.getRichStringCellValue());
                break;
        }
    }

    // If there are are any merged regions in the source row, copy to new row
    for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
        CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
        if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {
            CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),
                    (newRow.getRowNum() +
                            (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow()
                            )),
                    cellRangeAddress.getFirstColumn(),
                    cellRangeAddress.getLastColumn());
            worksheet.addMergedRegion(newCellRangeAddress);
        }
    }
}

调用者方法:

    FileInputStream excelFile = new FileInputStream(new File("C:\\CodeReview_Template.xlsx"));
    Workbook workbook = new XSSFWorkbook(excelFile);
    Sheet sheet = workbook.getSheet("Apex Code");
    copyRow(workbook, sheet, 2, 3);
    FileOutputStream out = new FileOutputStream("C:\\CodeReview_Template2.xlsx");
    workbook.write(out);
    out.close();

原始工作簿如下:

enter image description here

运行上面的代码后,它看起来像这样:

enter image description here

在原始工作簿中可以看到,我在行4的深蓝色处进行了移位,但是我不知道在哪里插入的新行不遵循样式上一行?

有什么我想念的吗?

0 个答案:

没有答案