如何将单元格注释复制到新单元格(复制整个工作表时)

时间:2013-07-19 16:22:46

标签: apache apache-poi

我正在尝试将CellComment复制到新单元格(使用Apache POI)。我知道我必须创建新的评论(而不是仅仅将旧评论设置到新单元格中),但我似乎无法找到如何复制锚点。

private void createComment(Cell aNewCell, Comment theOldComment) {
    CreationHelper createHelper = aNewCell.getSheet().getWorkbook().getCreationHelper();
    ClientAnchor anchor = createHelper.createClientAnchor();
    // How to get the old CellComment anchor for x1, c2, y1, y2???
    Drawing drawing = aNewCell.getSheet().createDrawingPatriarch();
    Comment aNewComment = drawing.createCellComment(anchor);
    aNewComment.setString(theOldComment.getString());
    aNewCell.setCellComment(aNewComment);
}

1 个答案:

答案 0 :(得分:0)

使用Patriarch代替CreationHelper。从HSSFPatriarch对象创建Anchor,并将值作为参数传递给createAnchor函数。希望这可以帮助。

 HSSFPatriarch drawing = sheet.createDrawingPatriarch();
 ClientAnchor anchor = drawing.createAnchor(10, 20, 30, 40, (short)4, 2, (short)6, 5);

希望这有帮助。

 private void createComment(HSSFCell aNewCell, Comment theOldComment) {
        HSSFPatriarch drawing = aNewCell.getSheet().getDrawingPatriarch();
        ClientAnchor anchor = drawing.createAnchor(drawing.getChildren().get(0).getAnchor().getDx1(),
                drawing.getChildren().get(0).getAnchor().getDx2(), drawing.getChildren().get(0).getAnchor().getDy1(),
                drawing.getChildren().get(0).getAnchor().getDy2(), (short)4, 2, (short)6, 5);
        Comment aNewComment = drawing.createCellComment(anchor);
        aNewComment.setString(theOldComment.getString());
        aNewCell.setCellComment(aNewComment);
    }