A comment is a rich text note which we often use to add tips or additional information to a particular Excel cell. Free Spire.XLS for Java library provides Java developers the ability to add and manipulate comments in Excel files within Java applications. In this article, we will introduce how to add, read and delete comments in Excel using Free Spire.XLS for Java library.
Installation
First of all, you need to download spire.xls.jar and add it to your project as dependency. If you use maven, you need to add the following dependencies to your 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 Comments
下面的示例演示如何使用免费的Java Spire.XLS将注释添加到Excel文件,并以不同的颜色设置注释文本中各个字符的格式。
import com.spire.xls.*;
public class AddComments {
public static void main(String[] args){
//Create a Workbook instance
Workbook workbook = new Workbook();
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Set worksheet name
sheet.setName("Comments");
//Add text to cell [1,1]
CellRange range = sheet.getCellRange(1,1);
range.setText("Working with comment:");
//Add text to cell [5,1]
CellRange range1 = sheet.getCellRange(5, 1);
range1.setText("Comment");
//Add comment to cell [5,1]
range1.getComment().setText("It's a comment.\nmulti-line,\nvisible and\nformatted.");
//Show comment
range1.getComment().setVisible(true);
//Set comment hight
range1.getComment().setHeight(100);
//Create fonts and set font color
ExcelFont fontBlue = workbook.createFont();
fontBlue.setKnownColor(ExcelColors.LightBlue);
ExcelFont fontGreen = workbook.createFont();
fontGreen.setKnownColor(ExcelColors.LightGreen);
//Set fonts for individual characters within the comment text
range1.getComment().getRichText().setFont(0, 4, fontGreen);
range1.getComment().getRichText().setFont(5, 6, fontBlue);
range1.getComment().getRichText().setFont(7, 15, fontGreen);
//Save the file
workbook.saveToFile("AddComments.xlsx", ExcelVersion.Version2013);
}
}
Read Comments
Java的免费Spire.XLS支持读取所有注释以及读取与Excel工作表中指定单元格相关的特定注释。
import com.spire.xls.*;
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 all of the comments in the worksheet
for(int i = 0; i < sheet.getComments().getCount(); i ++){
String comment = sheet.getComments().get(i).getText();
System.out.println(comment);
}
//Print out comment associated with a specified cell
//System.out.println(sheet.getCellRange(5,1).getComment().getText());
}
}
Delete Comments
我们可以删除所有注释,也可以删除与Excel工作表中指定单元格关联的特定注释。
import com.spire.xls.*;
public class DeleteComments {
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);
//Delete all of the comments
for(int i = 0; i < sheet.getComments().getCount(); i ++){
sheet.getComments().get(i).remove();
}
//Delete comment associated with a specified cell
sheet.getCellRange(5,1).getComment().remove();
workbook.saveToFile("DeleteComments.xlsx", ExcelVersion.Version2013);
}
}