Comments in Excel are used to explain the contents in cells, or to offer reminders/notes to other users. Once a comment’s been added, Microsoft Excel allows users to show, hide, edit and delete the comments easily in a worksheet. This article will introduce how to edit existing comments as well as remove comments programmatically using Spire.XLS for Java.
Install Spire.XLS for Java
First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls</artifactId> <version>14.9.8</version> </dependency> </dependencies>
Edit Comments in Excel
After adding comments to your Excel workbook, you may want to make changes to the added comments. The below table lists some of the core classes and methods used to get the existing comments and then set new text as well as formatting for the comments.
Name | Description |
ExcelCommentObject Class | Represents a comment in an Excel document. |
CellRange.getComment() Method | Returns a comment object that represents a comment in a specific cell range. |
ExcelCommentObject.setText() Method | Sets the comment text. |
ExcelCommentObject.setHeight() Method | Sets height of a comment. |
ExcelCommentObject.setWidth() Method | Sets width of a comment. |
ExcelCommentObject.setAutoSize() Method | Sets whether the size of the specified object is changed automatically to fit text within its boundaries. |
The following are steps to edit comments in Excel:
- Create a Workbook instance.
- Load an Excel file using Workbook.loadFromFile() method.
- Get the first worksheet of the Excel file using WorksheetsCollection.get() method.
- Get a comment in a specific cell range using CellRange.getComment() method and then set new text and height/width or auto size for the existing comment using the methods offered by ExcelCommentObject class.
- Save the document to another file using Workbook.saveToFile() method.
- Java
import com.spire.xls.*; public class ModifyComment { public static void main(String[] args) { //Create a Workbook instance Workbook wb = new Workbook(); //Load an Excel file wb.loadFromFile("Comments.xlsx"); //Get the first worksheet Worksheet sheet = wb.getWorksheets().get(0); //Get comments in specific cells and set new comments sheet.getRange().get("A8").getComment().setText("Frank has left the company."); sheet.getRange().get("F6").getComment().setText("Best sales."); //Set the height and width of the new comments sheet.getRange().get("A8").getComment().setHeight(50); sheet.getRange().get("A8").getComment().setWidth(100); sheet.getRange().get("F6").getComment().setAutoSize(true); //Save to file. wb.saveToFile("ModifyComment.xlsx",ExcelVersion.Version2013); wb.dispose(); } }
Remove Comments in Excel
The following are steps to remove a comment in Excel:
- Create a Workbook instance.
- Load an Excel file using Workbook.loadFromFile() method.
- Get the first worksheet of the Excel file using WorksheetsCollection.get() method.
- Get a comment in a specific cell range using CellRange.getComment() method and then delete the comment using ExcelCommentObject.remove() method.
- Save the document to another file using Workbook.saveToFile() method.
- Java
import com.spire.xls.*; public class DeleteComment { public static void main(String[] args) { //Create a Workbook instance Workbook wb = new Workbook(); //Load an Excel file wb.loadFromFile("Comments.xlsx"); //Get the first worksheet Worksheet sheet = wb.getWorksheets().get(0); //Get the comment in a specific cell and remove it sheet.getRange().get("F6").getComment().remove(); //Save to file. wb.saveToFile("DeleteComment.xlsx", ExcelVersion.Version2013); wb.dispose(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.