【问题标题】:creating cell comments using HSSFClientAnchor in apache poi在 apache poi 中使用 HSSFClientAnchor 创建单元格注释
【发布时间】:2010-02-17 14:00:15
【问题描述】:

有人可以向我解释在创建单元 cmets 时如何正确使用 Anchors 吗?我的工作正常,但是电子表格发生了变化,我在让我的单元格 cmets 出现时遇到了问题。这是我使用的有效代码:

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

【问题讨论】:

    标签: java apache-poi


    【解决方案1】:

    有点晚了,但这可能会起作用(它适用于我,而快速入门中的 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】:

    以下代码适用于我的 Office 2007 (xlsx) 格式文件。从 POI 指南中发现了这一点 http://poi.apache.org/spreadsheet/quick-guide.html#CellCommentsHow 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多