creating cell comments using HSSFClientAnchor in apache poi

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

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 getting my cell comments to appear. This is the code I was using that worked:

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

That was found mostly by experimenting around. Looking at the api for it doesn't exactly make it any clearer.

Based on the quick start guide I have also tried the following with no luck:

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

回答1:

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 drawingPatriarches = new HashMap();      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



回答2:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!