代码之家  ›  专栏  ›  技术社区  ›  Casey

在Apache POI中使用hssfclientanchor创建单元注释

  •  3
  • Casey  · 技术社区  · 14 年前

    有人能给我解释一下在创建单元格注释时如何正确使用锚吗?我的正在工作,但电子表格发生了变化,我的手机评论出现问题。这就是我使用的代码:

     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)); 
    
    2 回复  |  直到 13 年前
        1
  •  5
  •   Erik Pragt    14 年前

    有点晚了,但这可能会起作用(它对我有效,而快速启动的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);
    }
    

    埃里克普拉特

        2
  •  4
  •   Community Egal    7 年前

    以下代码适用于Office2007(XLSX)格式文件。从POI指南中了解到这一点 http://poi.apache.org/spreadsheet/quick-guide.html#CellComments How to set comments for 3 cells using apache poi

    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);
    }