creating cell comments using HSSFClientAnchor in apache poi

后端 未结 2 1640
情书的邮戳
情书的邮戳 2020-12-21 06:49

Could someone please explain to me how to properly use the Anchors when creating cell comments? Mine were working, but the spread sheet changed and I am having issues gettin

相关标签:
2条回答
  • 2020-12-21 07:29

    A bit late, but this will probably work (it works for me, while the Apache POI example from the quick start also didn't work for me):

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

    Erik Pragt

    0 讨论(0)
  • 2020-12-21 07:31

    The following code works for me for Office 2007 (xlsx) format files. Figured this out from POI guide http://poi.apache.org/spreadsheet/quick-guide.html#CellComments and 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);
    }
    
    0 讨论(0)
提交回复
热议问题