Excel comments are additional notes or commentary that can be added to specified cells to provide more in-depth explanations or to offer tips to other users. Once a comment’s been added, Excel provides users with the flexibility to format, edit, delete and show/hide the comment in the worksheet. In this article, you will learn how to programmatically edit or delete existing comments in Excel using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Edit Comments in Excel
After adding comments to your Excel workbook, you may sometimes need to make changes to the added comments. The below table lists some of the core classes and properties used to get the existing comments and then set new text as well as formatting for the comments.
Name | Description |
CellRange.Comment Property | Returns a Comment object that represents the comment associated with the cell in the upper-left corner of the range. |
ExcelCommentObject Class | Represents a comment. |
ExcelCommentObject.Text Property | Gets or sets the comment text. |
ExcelCommentObject.Height Property | Gets or sets height of a comment. |
ExcelCommentObject.Width Property | Gets or sets width of a comment. |
ExcelCommentObject.AutoSize Property | Indicates 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 Workbook.Worksheets[] property.
- Get a comment in a specific cell range using Worksheet.Range.Comment property.
- Set new text and height/width or auto size for the existing comment using the properties of ExcelCommentObject class.
- Save the document to another file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls; namespace EditExcelComment { class Program { static void Main(string[] args) { // Create a Workbook instance Workbook workbook = new Workbook(); // Load an Excel file workbook.LoadFromFile("Comments.xlsx"); // Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Get comments in specific cells and set new comments sheet.Range["A8"].Comment.Text = "Frank has left the company."; sheet.Range["F6"].Comment.Text = "Best sales."; // Set the height and width of the new comments sheet.Range["A8"].Comment.Height = 50; sheet.Range["A8"].Comment.Width = 100; sheet.Range["F6"].Comment.AutoSize = true; // Save to file. workbook.SaveToFile("ModifyComment.xlsx", ExcelVersion.Version2013); } } }
Delete Comments in Excel
The ExcelCommentObject.Remove() method offered by Spire.XLS for .NET allows you to remove a specified comment easily. The detailed steps are as follows:
- Create a Workbook instance.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get the first worksheet of the Excel file using Workbook.Worksheets[] property.
- Get a comment in a specific cell range using Worksheet.Range.Comment property and then delete the comment using ExcelCommentObject.Remove() method.
- Save the document to another file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls; namespace EditExcelComment { class Program { static void Main(string[] args) { // Create a Workbook instance Workbook workbook = new Workbook(); // Load an Excel file workbook.LoadFromFile("Comments.xlsx"); // Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Get the comment in a specific cell and remove it sheet.Range["F6"].Comment.Remove(); // Save to file. workbook.SaveToFile("DeleteComment.xlsx", ExcelVersion.Version2013); } } }
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.