3

有人可以向我解释在创建单元格评论时如何正确使用锚点吗?我的工作正常,但电子表格发生了变化,我无法让我的单元格评论出现。这是我使用的有效代码:

 Comment c = drawing.createCellComment (new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short)6, 5));

这主要是通过实验发现的。查看它的 api 并不能使它更清晰。

根据快速入门指南,我还尝试了以下方法,但没有成功:

ClientAnchor anchor = chf.createClientAnchor();
Comment c = drawing.createCellComment(anchor);
c.setString(chf.createRichTextString(message)); 
4

2 回答 2

5

有点晚了,但这可能会起作用(它适用于我,而快速入门中的 Apache POI 示例也不适用于我):

    public void setComment(String text, Cell cell) {
    final Map<Sheet, HSSFPatriarch> drawingPatriarches = new HashMap<Sheet, HSSFPatriarch>();

    CreationHelper createHelper = cell.getSheet().getWorkbook().getCreationHelper();
    HSSFSheet sheet = (HSSFSheet) cell.getSheet();
    HSSFPatriarch drawingPatriarch = drawingPatriarches.get(sheet);
    if (drawingPatriarch == null) {
        drawingPatriarch = sheet.createDrawingPatriarch();
        drawingPatriarches.put(sheet, drawingPatriarch);
    }

    Comment comment = drawingPatriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
    comment.setString(createHelper.createRichTextString(text));
    cell.setCellComment(comment);
}

埃里克·普拉格

于 2010-04-22T12:16:10.890 回答
4

以下代码适用于我的 Office 2007 (xlsx) 格式文件。从 POI 指南 http://poi.apache.org/spreadsheet/quick-guide.html#CellComments如何使用 apache poi 为 3 个单元格设置评论中发现了这一点

protected void setCellComment(Cell cell, String message) {
    Drawing drawing = cell.getSheet().createDrawingPatriarch();
    CreationHelper factory = cell.getSheet().getWorkbook()
            .getCreationHelper();
    // When the comment box is visible, have it show in a 1x3 space
    ClientAnchor anchor = factory.createClientAnchor();
    anchor.setCol1(cell.getColumnIndex());
    anchor.setCol2(cell.getColumnIndex() + 1);
    anchor.setRow1(cell.getRowIndex());
    anchor.setRow2(cell.getRowIndex() + 1);
    anchor.setDx1(100);
    anchor.setDx2(100);
    anchor.setDy1(100);
    anchor.setDy2(100);

    // Create the comment and set the text+author
    Comment comment = drawing.createCellComment(anchor);
    RichTextString str = factory.createRichTextString(message);
    comment.setString(str);
    comment.setAuthor("Apache POI");
    // Assign the comment to the cell
    cell.setCellComment(comment);
}
于 2011-07-04T14:47:49.400 回答