我的代码:
如何在XSSFCell中克隆Apache POI-在循环访问行时,我只想clone/copy呈现XSSFCell并想在新创建的Cell中进行一些更改。

          XSSFRow myRow = (XSSFRow)rowIter.next();
          Iterator cellIter = myRow.cellIterator();
          List cellRowList = new ArrayList();

          while (cellIter.hasNext())
          {
              XSSFCell myCell = (XSSFCell)cellIter.next();
              try
             {
                XSSFCell newCell = myCell //( How to clone or copy a new one)

             } catch(Exception e) {
                syso(e);
             }

           }

最佳答案

您可以使用cloneStyleFrom从原始工作簿中克隆样式。

XSSFRow myRow = (XSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
List cellRowList = new ArrayList();

while (cellIter.hasNext()) {
    XSSFCell originalCell = (XSSFCell) cellIter.next();
    XSSFCell myCell = originalCell;
    try {
        // XSSFCell newCell = myCell; //( How to clone or copy a new one)
        XSSFCellStyle style = workbook.createCellStyle(); // the XSSFWorkbook from which you clone
        style.cloneStyleFrom(originalCell.getCellStyle());
        myCell.setCellStyle(style);

    } catch (Exception e) {
        syso(e);
    }
}

10-05 18:40