Excel cell comments

The following example shows how you can set Excel cell comment with location, text and visibility.

Sample also shows how to format individual characters and words within a comment text.

Comments reading/writing is supported in XLSX, XLS and ODS file formats.

Comments formatting is supported only in XLSX.

Excel cell comments set with GemBox.Spreadsheet for Java
Screenshot of Excel cell comments set with GemBox.Spreadsheet for Java
import com.gembox.spreadsheet.*;

class Program {

    public static void main(String[] args) throws java.io.IOException {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.setLicense("FREE-LIMITED-KEY");

        ExcelFile workbook = new ExcelFile();
        ExcelWorksheet worksheet = workbook.addWorksheet("Comments");

        worksheet.getCell(0, 0).setValue("Comment examples:");

        worksheet.getCell(2, 1).getComment().setText("Empty cell.");

        worksheet.getCell(4, 1).setValue(5);
        worksheet.getCell(4, 1).getComment().setText("Cell with a number.");

        worksheet.getCell("B7").setValue("Cell B7");

        ExcelComment comment = worksheet.getCell("B7").getComment();
        comment.setText("Some formatted text.\nComment is:\na) multiline,\nb) large,\nc) visible, and \nd) formatted.");
        comment.setVisible(true);
        comment.setTopLeftCell(new AnchorCell(worksheet.getColumn(3), worksheet.getRow(4), true));
        comment.setBottomRightCell(new AnchorCell(worksheet.getColumn(5), worksheet.getRow(10), false));

        // Get first 20 characters of a string.
        FormattedCharacterRange characters = comment.getCharacters(0, 20);

        // Apply color, italic and size to selected characters.
        characters.getFont().setColor(SpreadsheetColor.fromName(ColorName.ORANGE));
        characters.getFont().setItalic(true);
        characters.getFont().setSize(300);

        // Apply color to 'formatted' part of text.
        comment.getCharacters(5, 9).getFont().setColor(SpreadsheetColor.fromName(ColorName.RED));

        workbook.save("Comments.%OutputFileType%");
    }
}

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Spreadsheet for Java yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.