Working with Comments

Working with Comments

Aspose.Cells FOSS for Java lets you add, read, modify, and remove cell comments (notes) in XLSX workbooks. Comments are accessed through the CommentCollection on a worksheet and are represented as Comment objects that carry the note text, author name, and display dimensions.


Overview

Each worksheet exposes its comments through Worksheet.getComments(), which returns a CommentCollection. You can add a comment by cell address or by row/column index, retrieve existing comments by index or cell name, and remove them when no longer needed.

A Comment stores the note text (getNote()/setNote()), the author (getAuthor()/setAuthor()), its anchor position (getRow(), getColumn()), its display dimensions (getWidth(), getHeight()), and whether it is visible by default (isVisible()/setVisible()).


Accessing Comments on a Worksheet

Retrieve the CommentCollection from any Worksheet via Worksheet.getComments(). Use CommentCollection.getCount() to determine how many comments are present. Individual comments are accessible by zero-based index through CommentCollection.get(index) or by cell name through CommentCollection.get(cellName).


Adding a Comment

Use CommentCollection.add(cellName) or CommentCollection.add(row, column) to insert a new comment. Both overloads return a Comment object. Set the note text and author on the returned instance before saving.

// Add a comment to cell B2 and configure it
Comment comment = ws.getComments().add("B2");
comment.setNote("Review this value before submission.");
comment.setAuthor("Analyst");
comment.setVisible(false);
comment.setWidth(180);
comment.setHeight(60);

Reading Comment Content

Iterate over all comments using the count:

CommentCollection comments = ws.getComments();
for (int i = 0; i < comments.getCount(); i++) {
    Comment c = comments.get(i);
    System.out.printf("Cell [%d,%d] author=%s note=%s%n",
        c.getRow(), c.getColumn(), c.getAuthor(), c.getNote());
}

Modifying an Existing Comment

Retrieve the comment by cell address and update its fields directly:

Comment c = ws.getComments().get("B2");
if (c != null) {
    c.setNote("Updated note text.");
    c.setVisible(true);
}

Removing a Comment

Use CommentCollection.removeAt(index) or CommentCollection.removeAt(cellName) to delete a comment:

ws.getComments().removeAt("B2");

Tips and Best Practices

  • Always check that CommentCollection.get(cellName) does not return null before modifying a comment — the cell may have no comment attached.
  • Set Comment.setVisible(false) for annotations that should only appear on hover, which matches standard Excel behavior.
  • Use Comment.setWidth() and Comment.setHeight() (in pixels) to ensure the note box is large enough for multi-line text.
  • Author names are stored as plain strings; they are not validated against the workbook’s user list.
  • Comments survive a load/save round-trip through XLSX when no modifications are made to CommentCollection.

Common Issues

IssueCauseFix
get(cellName) returns nullNo comment at that addressCheck the cell address or iterate by index
Comment note is empty after savesetNote() not called before saveCall setNote() on the returned Comment before saving the workbook
Comment not visible in ExcelsetVisible(false) was setChange to setVisible(true) or hover over the cell in Excel

FAQ

How do I add a comment at a specific row and column?

Use CommentCollection.add(row, column), where both values are zero-based indices. This is equivalent to add(cellName) with the corresponding cell address.

Can I change the author of an existing comment?

Yes. Retrieve the Comment via get(cellName) and call setAuthor(name) with the new author string.

How do I check whether a comment is currently visible?

Call Comment.isVisible(). Returns true if the comment box is always shown; false if it appears only on hover.

Are comments preserved when converting to other formats?

Comments are preserved on XLSX round-trip. Preservation behavior for other formats depends on format support in the target output.

How do I count comments on a worksheet?

Use Worksheet.getComments().getCount().


API Reference Summary

Comment

MethodReturn typeDescription
getRow()intZero-based row index of the anchor cell.
getColumn()intZero-based column index of the anchor cell.
getNote()StringThe comment text.
setNote(note)voidSets the comment text.
getAuthor()StringThe author name.
setAuthor(author)voidSets the author name.
isVisible()booleanWhether the comment box is always visible.
setVisible(visible)voidSets the visibility of the comment box.
getWidth()intWidth of the comment box in pixels.
setWidth(width)voidSets the width of the comment box.
getHeight()intHeight of the comment box in pixels.
setHeight(height)voidSets the height of the comment box.

CommentCollection

MethodReturn typeDescription
getCount()intNumber of comments on the worksheet.
get(index)CommentReturns the comment at the zero-based index.
get(cellName)CommentReturns the comment at the given cell address.
add(row, column)CommentAdds a new comment at the row/column position.
add(cellName)CommentAdds a new comment at the cell address.
removeAt(index)voidRemoves the comment at the zero-based index.
removeAt(cellName)voidRemoves the comment at the cell address.

See Also