News Category

Java: Insert or Format Comments in Excel

2022-03-01 01:45:00 Written by  support iceblue
Rate this item
(0 votes)

Comments in an Excel cell are commonly used for sharing extra information or reviews about the data inside the cell. Comments make it easy to remember, follow up, or reference the data in your worksheet. With Spire.XLS for Java, you can insert a comment to Excel as well as formatting a comment with easy. In this article, we will show you how to add a comment to your Excel spreadsheet in Java from the following three parts.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>14.3.2</version>
    </dependency>
</dependencies>
    

Add Comments in an Excel Worksheet

Spire.XLS offers the CellRange.addComment() method to insert the regular text comment to Excel worksheets.

  • Create a Workbook instance.
  • Load a sample Excel document using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Add a comment in a specific cell range using CellRange.addComment() method and then set the comment text through the ExcelComment.setText() method.
  • Save the document to another file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

public class Excelcomment {
    public static void main(String[] args) throws Exception {

        //Load the sample document from file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Add regular comment to specific cell range C6
        CellRange range = sheet.getCellRange("C6");
        ExcelComment comment = range.addComment();
        comment.setVisible(true);
        comment.setText("Regular comment");

        //Save the document to another file
        workbook.saveToFile("Addcomment.xlsx", ExcelVersion.Version2016);

    }
}

Java: Insert or Format Comments in Excel

Apply Formatting to Comments in an Excel Worksheet

Spire.XLS offers the Comment.getRichText().setFont() method to apply font formatting for comments in Excel worksheets.

  • Initialize an instance of Workbook class and load an Excel file using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Add a comment in a specific cell range using CellRange.addComment() method and then set the comment text.
  • Create an ExcelFont object and apply the font to the comment text using ExcelComment.getRichText.setFont() method.
  • Save the document to another file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

import java.awt.*;

public class Excelcomment {
    public static void main(String[] args) throws Exception {

        //Load the sample document from file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Create font
        ExcelFont font = workbook.createFont();
        font.setFontName("Calibri");
        font.setSize(12);
        font.setColor(Color.orange);
        font.isBold(true);

        //Add regular comment to specific cell range C6
        CellRange range = sheet.getCellRange("C6");
        ExcelComment comment = range.addComment();
        comment.setVisible(true);
        comment.setHeight(100);
        comment.setWidth(200);
        comment.getRichText().setText("Spire.XLS for Java Rich Text Comment ");
        comment.getRichText().setFont(0, 40, font);
        comment.setTextRotation(TextRotationType.LeftToRight);

        //Set the alignment of text in Comment
        comment.setVAlignment(CommentVAlignType.Center);
        comment.setHAlignment(CommentHAlignType.Justified);


        //Save the document to another file
        workbook.saveToFile("AddRichTextcomment.xlsx", ExcelVersion.Version2016);

    }
}

Java: Insert or Format Comments in Excel

Add Comment with Author in an Excel Worksheet

Spire.XLS offers the ExcelComment.setText() method to insert the comment with author to Excel worksheets.

  • Create a Workbook instance.
  • Load a sample Excel document using Workbook.loadFromFile() method.
  • Get a specified worksheet using Workbook.getWorksheets().get() method.
  • Add a comment in a specific cell range using CellRange.addComment() method.
  • Define the comment text and author and then add them as the comment content through the ExcelComment.setText() method.
  • Save the document to another file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;

public class Excelcomment {
    public static void main(String[] args) throws Exception {

        //Load the sample document from file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Add regular comment to specific cell range C6
        CellRange range = sheet.getCellRange("C6");
        ExcelComment comment = range.addComment();
        comment.setVisible(true);

        //Set the author and comment content
        String text = "Add a comment with Author";
        String author = "E-iceblue:";
        comment.setText(author + "\r" + text);

        //Save the document to another file
        workbook.saveToFile("Addcomment.xlsx", ExcelVersion.Version2016);

    }
}

Java: Insert or Format Comments in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Tuesday, 27 September 2022 02:30