Document Solutions for Excel, Java Edition | Document Solutions
Features / Comments
In This Topic
    Comments
    In This Topic

    DsExcel Java enables you to annotate a worksheet by writing comments on cells in order to specify additional information about the data it contains.

    For instance, let us assume you want to enter only the numeric information in an individual cell of a worksheet. To accomplish this, instead of populating a small cell with large notes, it is more ideal to use a short comment (something like "Please enter only numeric characters in this cell") in order to provide additional context for the data represented in that cell.

    The cells annotated with comments will display a small red indicator (at the corner of the cell) which appear when your mouse pointer is placed on that particular cell. The text in the comments can be edited, copied and formatted. Also, the comments can be moved, resized or deleted, can be made hidden or visible and their indicators can also be customized as per your preferences.

    Comment

     The following tasks can be performed while applying comments in cells of a spreadsheet:

    Add comment to a cell

    In DsExcel Java, a cell comment instance is represented by the IComment interface. You can insert comment to a cell or a range of cells using the addComment method of the IRange interface. You can also set the text of the comment using the setText method of the IComment interface.

    Refer to the following example code to add comment to a cell.

    Java
    Copy Code
    // Add new comments 
    IComment commentC3 = worksheet.getRange("C3").addComment("Range C3's comment.");
    IComment commentC4 = worksheet.getRange("C4").addComment("Range C4's comment.");
    IComment commentC5 = worksheet.getRange("C5").addComment("Range C5's comment.");
            
    // Change the text of a comment.
    commentC3.setText("New Comment for Range C3.");

    Set comment layout

    You can configure the layout of the comment added to an individual cell or a range of cells using getShape method of the IComment interface.

    Refer to the following example code to set comment layout.

    Java
    Copy Code
    IComment commentC3 = worksheet.getRange("C3").addComment("Range C3's comment.");
    // Configure comment layout
    commentC3.getShape().getLine().getColor().setRGB(Color.GetGreen());
    commentC3.getShape().getLine().setWeight(7);
    commentC3.getShape().getLine().setStyle(LineStyle.ThickThin);
    commentC3.getShape().getLine().setDashStyle(LineDashStyle.Solid);
    commentC3.getShape().getFill().getColor().setRGB(Color.GetGray());
    commentC3.getShape().setWidth(100);
    commentC3.getShape().setHeight(200);
    commentC3.getShape().getTextFrame().getTextRange().getFont().setBold(true);
    commentC3.setVisible(true);

    Show/Hide comment

    You can choose to keep comments hidden or visible by using the setVisible method of the IComment interface.

    In order to show/hide comment added to a cell, refer to the following example code.

    Java
    Copy Code
    // Add comment.
    IComment commentC3 = worksheet.getRange("C3").addComment("Range C3's comment.");
    // Show comment
    commentC3.setVisible(true);
    // Hide comment
    commentC3.setVisible(false);

    Author comments

    You can represent the author of the comment by using the getAuthor method of the IComment interface. Also, you can use this method to change the author of an existing comment.

    In order to set comment author for a cell, refer to the following example code.

    Java
    Copy Code
    // Set comment author
    workbook.setAuthor("joneshan");        
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    worksheet.getRange("C3").addComment("Range C3's comment.");
    // C3's comment author is "joneshan'
    String authorC3 = worksheet.getRange("C3").getComment().getAuthor();
    System.out.println(authorC3);

    Set rich text for comment

    You can set the rich text for the comment using the methods of the ITextFrame interface that control the text style.

    In order to set rich text for the comment, refer to the following example code.

    Java
    Copy Code
    // Add a new comment
    IComment commentC3 = worksheet.getRange("C3").addComment("Range C3's comment.");
    
    // Configure the paragraph style using Rich Text and Text Frame.
    commentC3.getShape().getTextFrame().getTextRange().getParagraphs().get(0).getFont().setBold(true);
    commentC3.getShape().getTextFrame().getTextRange().getParagraphs().get(0).getRuns().add("ccc", 0);
    commentC3.getShape().getTextFrame().getTextRange().getParagraphs().get(0).getRuns().add("eee", 1);
    commentC3.getShape().getTextFrame().getTextRange().getParagraphs().get(0).getRuns().add("ddd");
    commentC3.getShape().getTextFrame().getTextRange().getParagraphs().get(0).getRuns().get(2).getFont()
            .setItalic(true);
    commentC3.getShape().getTextFrame().getTextRange().getParagraphs().get(0).getRuns().get(2).getFont().getColor()
            .setRGB(Color.GetGreen());

    Delete comment

    You can delete the comment added to a cell or to a cell range using the delete method of the IComment interface and the IRange interface respectively.

    In order to delete comment from a cell, refer to the following example code.

    Java
    Copy Code
    // Add comments
     worksheet.getRange("C3").addComment("Range C3's comment.");
     worksheet.getRange("D4").addComment("Range D4's comment.");
     worksheet.getRange("D5").addComment("Range D5's comment.");
    
    // Delete a single cell comment
     worksheet.getRange("D5").getComment().delete();
    
    // Clear multiple comments from a range of cells 
     worksheet.getRange("C3:D4").clearComments();