2
votes

I am using Apache POI 3.9 to create cell comments.

I have been using the code for HSSF sheets suggested by Erik Pragt in creating cell comments using HSSFClientAnchor in apache poi for a few years, and it works well.

However now I have a need to add cell comments to XSSF sheets.

I have tried the code suggested by lastnitescurry in the same page, which works nicely, but it creates for me comments with a transparent background.

The code is reproduced below.

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

How can I change the background to a yellow background?

NOTE: If one edits the apache-poi created comment in Excel, then it will appear with a yellow background temporarily. However if one tries to format this comment to change the background from within Excel, then one can't. (The Color and Lines menu does not appear)

2

2 Answers

0
votes

The answer is that my Java was manipulating an Excel .xlsm file that had "Show All Comments" set to true. Once I changed this setting, the java created the comments correctly.

0
votes

I managed to reproduce the described behavior in a sheet that contained a Button Form Control. I deleted the button and the comments are showing correctly.

As an alternative to a button, I used this guide to run a Macro from a cell https://www.extendoffice.com/documents/excel/4354-excel-click-on-cell-to-run-macro.html