How to insert a row between two rows in an existing excel with HSSF (Apache POI)

前端 未结 8 1056
自闭症患者
自闭症患者 2020-11-27 02:38

Somehow I manage to create new rows between two rows in an existing excel file. The problem is, some of the formatting were not include along the shifting of the rows.

8条回答
  •  被撕碎了的回忆
    2020-11-27 03:21

    As to formulas being "updated" in the new row, since all the copying occurs after the shift, the old row (now one index up from the new row) has already had its formula shifted, so copying it to the new row will make the new row reference the old rows cells. A solution would be to parse out the formulas BEFORE the shift, then apply those (a simple String array would do the job. I'm sure you can code that in a few lines).

    At start of function:

    ArrayList fArray = new ArrayList();
    Row origRow = sheet.getRow(sourceRow);
    for (int i = 0; i < origRow.getLastCellNum(); i++) {
        if (origRow.getCell(i) != null && origRow.getCell(i).getCellType() == Cell.CELL_TYPE_FORMULA) 
            fArray.add(origRow.getCell(i).getCellFormula());
        else fArray.add(null);
    }
    

    Then when applying the formula to a cell:

    newCell.setCellFormula(fArray.get(i));
    

提交回复
热议问题