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 returnnullbefore 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()andComment.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
| Issue | Cause | Fix |
|---|---|---|
get(cellName) returns null | No comment at that address | Check the cell address or iterate by index |
| Comment note is empty after save | setNote() not called before save | Call setNote() on the returned Comment before saving the workbook |
| Comment not visible in Excel | setVisible(false) was set | Change 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
| Method | Return type | Description |
|---|---|---|
getRow() | int | Zero-based row index of the anchor cell. |
getColumn() | int | Zero-based column index of the anchor cell. |
getNote() | String | The comment text. |
setNote(note) | void | Sets the comment text. |
getAuthor() | String | The author name. |
setAuthor(author) | void | Sets the author name. |
isVisible() | boolean | Whether the comment box is always visible. |
setVisible(visible) | void | Sets the visibility of the comment box. |
getWidth() | int | Width of the comment box in pixels. |
setWidth(width) | void | Sets the width of the comment box. |
getHeight() | int | Height of the comment box in pixels. |
setHeight(height) | void | Sets the height of the comment box. |
CommentCollection
| Method | Return type | Description |
|---|---|---|
getCount() | int | Number of comments on the worksheet. |
get(index) | Comment | Returns the comment at the zero-based index. |
get(cellName) | Comment | Returns the comment at the given cell address. |
add(row, column) | Comment | Adds a new comment at the row/column position. |
add(cellName) | Comment | Adds a new comment at the cell address. |
removeAt(index) | void | Removes the comment at the zero-based index. |
removeAt(cellName) | void | Removes the comment at the cell address. |