Logo

dev-resources.site

for different kinds of informations.

Java: Add and Read Comments in Excel

Published at
10/11/2021
Categories
java
excel
comments
api
Author
codesharing
Categories
4 categories in total
java
open
excel
open
comments
open
api
open
Author
11 person written this
codesharing
open
Java: Add and Read Comments in Excel

Excel comments can serve for various purposes, such as explaining the contents in cells, offering tips to other users, or cross-referencing with other Excel workbooks. This article will share how to add comments to an Excel file and read the comments using Free Spire.XLS for Java.

Import JAR Dependency (2 Method)
1# Download the free library and unzip it, then add the Spire.Xls.jar file to your project as dependency.
2# You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<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>3.9.1</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Add Comments
The following is the sample code of adding a regular comment and a rich text comment to an Excel file

import com.spire.xls.*;

public class InsertComments {
    public static void main(String[] args){
        //Load a Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("Sales1.xlsx");

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

        //Create fonts
        ExcelFont font = workbook.createFont();
        font.setFontName("Arial");
        font.setSize(11);
        font.setKnownColor(ExcelColors.Orange);
        ExcelFont fontBlue = workbook.createFont();
        fontBlue.setKnownColor(ExcelColors.LightBlue);
        ExcelFont fontRed = workbook.createFont();
        fontRed.setKnownColor(ExcelColors.Red);

        //Add regular comment to specific cell range
        CellRange range = sheet.getCellRange("A8");
        range.getComment().setText("A new employee.");
        range.autoFitColumns();

        //Add rich text comment to specific cell range
        range = sheet.getCellRange("F8");
        range.getComment().getRichText().setText("Best sales of the month.");
        range.getComment().getRichText().setFont(0, 10, fontRed);
        range.getComment().getRichText().setFont(17, 23, fontBlue);

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

addcomments

Read Comments

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

public class ReadComments {
    public static void main(String[] args){
        //Load Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("AddComments.xlsx");
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);
        //Print out the comment
        System.out.println("The first comment: " + sheet.getCellRange("A8").getComment().getText());
        System.out.println("The second comment: " + sheet.getCellRange("F8").getComment().getRichText().getRtfText());
    }
}
Enter fullscreen mode Exit fullscreen mode

readcomments

comments Article's
30 articles in total
Favicon
Adding Structured Data in Astro's Starlight Documentation Framework
Favicon
Comments are not Evil: Beyond Self-Documenting Code 😈
Favicon
No comments. Now what?
Favicon
No comments. Now what?
Favicon
2 Best Methods to Enable Comments In Adobe PDF
Favicon
Generate references table from code comments
Favicon
The best comments are the ones we don't write
Favicon
How to make good use of comments
Favicon
Comments in Javascript
Favicon
Add comments to your Gridsome app!
Favicon
Quick response to Instagram comments with Facebook Inbox
Favicon
Making Comments Count
Favicon
Composing Compelling Code Comments: "I don't need to write comments, my code is self-documenting"
Favicon
Java: Add and Read Comments in Excel
Favicon
Python Language of Future?
Favicon
On Comments in Code
Favicon
Comments
Favicon
How to Add Commento (or other Comments) to Gatsby
Favicon
Add comments to your 11ty blog with utterances
Favicon
Don't use comments! Use code.
Favicon
Multiline Comments and Docstrings with VS Code
Favicon
On the journey to writing better comments
Favicon
To write code comments or not, it should not be a question
Favicon
Adding Comments to Your Vue Application with Hyvor Talk
Favicon
To comment or to not comment?
Favicon
Code Comments Are Stupid
Favicon
The Reason I Move Away From Disqus
Favicon
Comments and Docstrings in Python
Favicon
Comments Suck and You Probably Write Too Many
Favicon
Top 3 commenting systems for your next coding blog

Featured ones: