Add, read, and delete Excel comments in Java

Annotations are rich text annotations commonly used to add prompts or additional information to a given Excel cell. Free Spire.XLS for Java provides developers with 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. ** Free Spire.XLS for Java Download and unzip the package. ** 2. ** Import the Spire.Xls.jar package in the lib folder into your Java application as a dependency, 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 AddComments / ReadComments / DeleteComments here) and enter and run 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 use Free Spire.XLS for Java to add comments to an Excel file, and set each character in the annotation text to a different font color.

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");

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

        //Add text to 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\n can be multiple lines");

        //Show annotations
        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);

        //Sets 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 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){
        //Import an Excel document
        Workbook workbook = new Workbook();
        workbook.loadFromFile("AddComments.xlsx");

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

        //Print all 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);
        }

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

ReadComments.jpg

** Delete Excel comment ** You can use Free Spire.XLS for Java to delete all comments and even specific comments associated with a specified cell in an Excel worksheet.

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

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

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

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

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

DeleteComments.jpg

Recommended Posts

Add, read, and delete Excel comments in Java
Delete blank rows / columns in Java Excel
Comments in Java source
Read binary files in Java 1
Save Java PDF in Excel
Importing Excel data in Java 2
Read standard input in Java
Import Excel data in Java
Read binary files in Java 2
Importing Excel data in Java 3
Correct the character code in Java and read from the URL
Easily read text files in Java (Java 11 & Java 7)
Java Excel Insertion and Image Extraction
Read Java properties file in C #
Encoding and Decoding example in Java
StringBuffer and StringBuilder Class in Java
Add, replace, delete Java PDF images
Understanding equals and hashCode in Java
Java adds and reads Excel formulas
Hello world in Java and Gradle
Sample to read and write LibreOffice Calc fods file in Java 2021
Difference between final and Immutable in Java
[Java] for Each and sorted in Lambda
Java creates a pie chart in Excel
Java (add2)
Read Felica using RC-S380 (PaSoRi) in Java
Java (add)
Read xlsx file in Java with Selenium
Arrylist and linked list difference in java
Add footnotes to Word documents in Java
Program PDF headers and footers in Java
Learn Flyweight patterns and ConcurrentHashMap in Java
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio
Reading and writing gzip files in Java
Difference between int and Integer in Java
Discrimination of Enums in Java 7 and above
Add SameSite attribute to cookie in Java
Create barcodes and QR codes in Java PDF
Read a string in a PDF file with Java
Detect similar videos in Java and OpenCV rev.2
Add --enable-preview option in Java in Visual Studio Code
Parallel and parallel processing in various languages (Java edition)
Difference between next () and nextLine () in Java Scanner
Differences in writing Java, C # and Javascript classes
Capture and save from selenium installation in Java
Detect similar videos in Java and OpenCV rev.3
Check static and public behavior in Java methods
[Java] Understand in 10 minutes! Associative array and HashMap
Basics of threads and Callable in Java [Beginner]
[Java] Read the file in src / main / resources
Distinguish between positive and negative numbers in Java
Java adds and removes watermarks in word documents
Detect similar videos in Java and OpenCV rev.1
Represents "next day" and "previous day" in Java / Android
Questions in java exception handling throw and try-catch
Upload and download notes in java on S3
Encrypt / decrypt with AES256 in PHP and Java
Generate OffsetDateTime from Clock and LocalDateTime in Java
Partization in Java
Changes in Java 11