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);
}
}
** 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());
}
}
** 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);
}
}
Recommended Posts