[Java] Add, read and delete Excel comments with Java

2 minute read

Annotations are rich text annotations commonly used to add prompts or additional information to specified Excel cells. Free Spire.XLS for Java gives developers the ability to add and manipulate comments in Excel files for free in Java applications. This article will show you how to use Free Spire.XLS for Java to add, read and delete comments in Excel documents.

Basic steps: 1. Download Free Spire.XLS for Java package and unzip it. 2. Import the Spire.Xls.jar package in the lib folder as a dependency into your Java application or install the JAR package from the Maven repository (see below for the code that makes up the pom.xml file) please). 3. In your Java application, create a new Java Class (named here AddComments / ReadComments / DeleteComments) and enter and execute the corresponding Java code.

Configure the pom.xml file:

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

Add Excel comment The following example shows how to add a comment to an Excel file using Free Spire.XLS for Java and also sets a different font color for each character in the annotation text.

import com.spire.xls.*;
public class AddComments {
    public static void main(String[] args){
        // New Excel document
        Workbook workbook = new Workbook();

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

        //Set worksheet name
        sheet.setName("Annotation");

        //Add text to cell [1,1]
        CellRange range = sheet.getCellRange(1,1);
        range.setText("Add comment:");

        //Add text to the cell [5,1]
        CellRange range1 = sheet.getCellRange(5, 1);
        range1.setText("annotation");

        //Add comment to cell [5,1]
        range1.getComment().setText("This is a comment\ncan be multiple lines");

        // display the annotation
        range1.getComment().setVisible(true);

        // set the height of the annotation
        range1.getComment().setHeight(100);

        // create a font and set the font color
        ExcelFont fontBlue = workbook.createFont();
        fontBlue.setKnownColor(ExcelColors.LightBlue);
        ExcelFont fontGreen = workbook.createFont();
        fontGreen.setKnownColor(ExcelColors.LightGreen);

        // Set the font for each character in the annotation text
        range1.getComment().getRichText().setFont(0, 3, fontGreen);
        range1.getComment().getRichText().setFont(4, 6, fontBlue);
        range1.getComment().getRichText().setFont(7, 9, fontGreen);

        // save the result file
        workbook.saveToFile("AddComments.xlsx", ExcelVersion.Version2013);
    }
}

AddComments.jpg

Read Excel comments Free Spire.XLS for Java supports reading all annotations and also specific comments associated with a specified cell in an Excel worksheet.

import com.spire.xls.*;
public class ReadComments {
    public static void main(String[] args){
        // Read the Excel document
        Workbook workbook = new Workbook();
        workbook.loadFromFile("AddComments.xlsx");

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

        // print all the comments contained in the worksheet
        for(int i = 0; i <sheet.getComments().getCount(); i ++){
            String comment = sheet.getComments().get(i).getText();
            System.out.println(comment);
        }

        //Print the comment associated with the specified cell
        //System.out.println(sheet.getCellRange(5,1).getComment().getText());
    }
}

ReadComments.jpg

Delete Excel comment With Free Spire.XLS for Java you can delete all the comments and also the specific comments associated with a specified cell in an Excel worksheet.

import com.spire.xls.*;
public class DeleteComments {
    public static void main(String[] args){
        // Read the Excel document
        Workbook workbook = new Workbook();
        workbook.loadFromFile("AddComments.xlsx");

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

        // remove all comments on the worksheet
        for(int i = 0; i <sheet.getComments().getCount(); i ++){
            sheet.getComments().get(i).remove();
        }

        // remove the comment associated with the specified cell
        sheet.getCellRange(5,1).getComment().remove();

        workbook.saveToFile("DeleteComments.xlsx", ExcelVersion.Version2013);
    }
}

DeleteComments.jpg