Image that you have received an Excel document from another person and want to leave your feedback or make corrections. Adding a comment to a particular cell in the Excel worksheet is often the best method as it allows you to attach additional information without changing the original data. This article will demonstrate how to programmatically add or delete comments in Excel worksheets using Java codes.
DEPENDENCY
In this code sample, you need a free third-party library called Free Spire.XLS for Java. Download it from this link, and then add Spire.Xls.jar to your Java program. Or if you use Maven, type the following code in the pom.xml file to easily import the JAR 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>5.1.0</version>
</dependency>
</dependencies>
Add a Comment to an Excel
Worksheet
Free Spire.XLS for Java offers the CellRange.addComment() method to add a text comment to an Excel worksheet. The following are detailed steps.
l Create a Workbook instance.
l Load an Excel sample document using Workbook.loadFromFile() method.
l Get a specified worksheet using Workbook.getWorksheets().get() method.
l Add a comment to a specific cell range
using CellRange.addComment() method
and then set the comment content through the ExcelComment.setText() method.
l Save the document to another file using Workbook.saveToFile() method.
import com.spire.xls.*;
public class AddComment {
public static void main(String[] args) {
//Load an Excel sample document
Workbook workbook = new Workbook();
workbook.loadFromFile("C:\\Users\\Tina\\Desktop\\sample.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Add regular comment to specific cell range D3
CellRange range = sheet.getCellRange("D3");
ExcelComment comment = range.addComment();
comment.setVisible(true);
//Set the author and comment content
String text = "It's located in the west of the Pacific Ocean";
String author = "Alex:";
comment.setText(author + "\r" + text);
//Save the document to another file
workbook.saveToFile("output/Addcomment.xlsx", ExcelVersion.Version2016);
}
}
Remove the Comment from an
Excel Worksheet
The following are steps to remove a comment from an Excel worksheet:
l Create a Workbook instance.
l Load an Excel sample file using Workbook.loadFromFile() method.
l Get a specific worksheet using WorksheetsCollection.get() method.
l Get a comment in a specific cell range
using CellRange.getComment() method
and then delete the comment using ExcelCommentObject.remove()
method.
l Save the document to another file using Workbook.saveToFile() method.
import com.spire.xls.*;
public class RemoveComment {
public static void main(String[] args) {
//Create a Workbook instance
Workbook wb = new Workbook();
//Load an Excel sample file
wb.loadFromFile("C:\\Users\\Tina\\Desktop\\Addcomment.xlsx");
//Get the first worksheet
Worksheet sheet = wb.getWorksheets().get(0);
//Get the comment in a specific cell and remove it
sheet.getRange().get("D3").getComment().remove();
//Save to file
wb.saveToFile("output/DeleteComment.xlsx", ExcelVersion.Version2013);
wb.dispose();
}
}
No comments:
Post a Comment