DEV Community

Alexis
Alexis

Posted on

Java - Insert, Edit or Delete Comments in Excel

A comment in Excel is a note that can be inserted into any cell. It can be used for many purposes, for example, to explain a formula, to give suggestions, or to provide notes for others. Once a comment is inserted, users can edit or delete it as per their requirements. In this article, I will demonstrate how to programmatically insert, edit or delete comments in Excel in Java using Free Spire.XLS for Java API.

Add Dependencies

Method 1: If you are using maven, you can easily import the JAR file of Free Spire.XLS for Java API into 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.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Method 2: If you are not using maven, you can download Free Spire.XLS for Java from the official website, extract the zip file, then import the Spire.Xls.jar file under the lib folder into your project as a dependency.

Insert Comments into Excel using Java

Free Spire.XLS for Java provides a Worksheet.getCellRange("rangeName").addComment() method for adding a comment to a specific cell. Once the comment is added, you can set text, formatting for the comment or fill the comment with image using the methods provided by the ExcelComment class.

The following example shows you how to add a regular text comment, add a comment with formatting and add a comment with image in Java using Free Spire.XLS for Java:

import com.spire.xls.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class AddPlainTextComment {
    public static void main(String[] args) throws IOException {
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("Test.xlsx");

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

        //Add a comment to cell C6
        ExcelComment textComment = sheet.getCellRange("B6").addComment();
        //Set comment text
        textComment.setText("Also known as the panda bear or simply the panda.");
        //Set comment location
        textComment.setCommentLocation(true, false);
        //Show the comment
        textComment.setVisible(true);

        //Create a font
        ExcelFont font = workbook.createFont();
        font.setFontName("Calibri");
        font.setSize(12);
        font.setColor(Color.orange);
        font.isBold(true);
        //Add a comment to cell F6
        ExcelComment richTextComment = sheet.getCellRange("F6").addComment();
        //Set the height and width for the comment
        richTextComment.setHeight(100);
        richTextComment.setWidth(200);
        //Set comment text
        richTextComment.getRichText().setText("The giant panda is a bear species endemic to China.");
        //Set comment font
        richTextComment.getRichText().setFont(0, 50, font);
        richTextComment.setTextRotation(TextRotationType.LeftToRight);
        //Set the alignment for the comment text
        richTextComment.setVAlignment(CommentVAlignType.Center);
        richTextComment.setHAlignment(CommentHAlignType.Justified);
        //Set comment location
        richTextComment.setCommentLocation(true, false);
        //Show the comment
        richTextComment.setVisible(true);

        //Add a comment to cell B12
        ExcelComment imageComment = sheet.getCellRange("B12").addComment();
        //Load an image
        BufferedImage bufferedImage = ImageIO.read(new File("Panda.jpg"));
        //Use the image to fill the comment
        imageComment.getFill().customPicture(bufferedImage, "Panda.jpg");
        //Set the height and width for the comment
        imageComment.setHeight(bufferedImage.getHeight()/4);
        imageComment.setWidth(bufferedImage.getWidth()/4);
        //Set comment location
        imageComment.setCommentLocation(true, false);
        //Show the comment
        imageComment.setVisible(true);

        //Save the result file
        workbook.saveToFile("AddComments.xlsx", ExcelVersion.Version2016);
    }
}
Enter fullscreen mode Exit fullscreen mode

Add comments to Excel using Java

Edit Comments in Excel using Java

If you want to edit the comment of a specific cell, you can use the Worksheet.getCellRange("rangeName").getComment() method to get the comment of the cell, then edit the text, height, width or other properties of the comment using the methods of the ExcelComment class.

The following example shows you how to edit the comments of specific cells in an Excel worksheet:

import com.spire.xls.ExcelComment;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class EditComment {
    public static void main(String[] args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();

        //Load an Excel file
        workbook.loadFromFile("AddComments.xlsx");

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

        //Get the comment of cell B6
        ExcelComment comment1 = sheet.getCellRange("B6").getComment();
        //Change the comment text
        comment1.setText("I have never seen a panda in the wild.");
        //Change the height and width of the comment
        comment1.setHeight(100);
        comment1.setWidth(150);
        comment1.setVisible(true);

        //Get the comment of cell F6
        ExcelComment comment2 = sheet.getCellRange("F6").getComment();
        //Change the size of the comment
        comment2.setAutoSize(true);

        //Save the result file
        workbook.saveToFile("EditComments.xlsx", ExcelVersion.Version2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Edit Comments in Excel using Java

Delete Comments in Excel using Java

You can delete the comment from a specific cell by using the Worksheet.getCellRange("rangeName").getComment().remove() method or delete all the comments from a specific worksheet by using the Worksheet.getComments().Clear() method.

The following example shows how to delete all the comments from an Excel worksheet:

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class DeleteComment {
    public static void main(String[] args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load an Excel file
        workbook.loadFromFile("AddComments.xlsx");

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

        //Delete the comment from cell F6
        //sheet.getCellRange("F6").getComment().remove();

        //Delete all the comments from the worksheet
        sheet.getComments().clear();

        //Save the result file
        workbook.saveToFile("DeleteComments.xlsx", ExcelVersion.Version2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Delete Comments in Excel using Java

Top comments (0)