creating cell comments using HSSFClientAnchor in apache poi
apache-poi, java
Solution
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
Problem
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)); ```