views:

451

answers:

3

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

Hi Casey,

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

Erik Pragt
This is basically what I ended up doing.
Casey
+1  A: 

Hello Erik! I used your code, it is very nice, but I have a problem. When I use it on several cells, only the last one will have a comment.

I have a loop with:

        cDest.setCellValue(cSource.getRichStringCellValue());
        setComment(rSource.getCell(1).getStringCellValue(),cDest);

All cells have the correct value, but comment does not work :(

Do you have any idea? I believe that the comment is always the same object and get moves from cell to cells?

best regards

Sylvain Lavelle
A: 

Hi Sylvain,

I had the same problem as you with the Erik's code, but then I realized the map should be outside of the method and this fixed the issue.

Regards

Andrey Ashikhmin