![Add Comments in Excel Worksheet C#](images/Add-Comments-in-Excel.jpg#center)
Comments in Excel worksheets are used to add additional information or to explain a formula. These comments can be formatted as well by defining the font size, height, width, etc. In this article, you will learn how to add comments to the Excel worksheets using C#. Furthermore, the article will also demonstrate how to apply formatting and add images to the comments programmatically.
- C# API to Add Comments in Excel
- Add Comments in an Excel Worksheet
- Add Image to a Comment in Excel
- Apply Formatting to Comments in Excel
C# API to Add Comments in Excel
In order to add comments in Excel worksheets, we will use Aspose.Cells for .NET. It is a powerful spreadsheet manipulation API that lets you create Excel files from scratch. Also, it supports modifying and converting existing Excel files seamlessly. You can either download the API or install it using NuGet.
PM> Install-Package Aspose.Cells
Add Comments to an Excel Worksheet in C#
The following are the steps to add comments to an Excel worksheet in C#.
- Load the Excel file using Workbook class.
- Get reference of the desired Worksheet from Workbook.Worksheets collection.
- Add comment to the worksheet using Worksheet.Comments.Add(string) method and get the reference of the comment in a Comment object.
- Set comment’s note using Comment.Note property.
- Save the updated Excel file using Workbook.Save(string) method.
The following code sample shows how to add a comment in an Excel worksheet using C#.
// Instantiating a Workbook object | |
Workbook workbook = new Workbook("workbook.xlsx"); | |
// Obtaining the reference of the first worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding a comment to "F5" cell | |
int commentIndex = worksheet.Comments.Add("F5"); | |
// Accessing the newly added comment | |
Comment comment = worksheet.Comments[commentIndex]; | |
// Setting the comment note | |
comment.Note = "Hello Aspose!"; | |
// Saving the Excel file | |
workbook.Save("output.xlsx"); |
Add Image to a Comment in Excel using C#
You can also add an image to the comment in the Excel worksheet following the below steps.
- Load the Excel file using Workbook class.
- Get reference of the CommentCollection in the desired worksheet using Workbook.Worksheets[index].Comments property.
- Add a new comment to the collection and get its reference in Comment object.
- Set comment’s note using Comment.Note property.
- Load the image from file into a Bitmap object.
- Save the Bitmap into a MemoryStream object.
- Add image to the comment using Comment.CommentShape.Fill.ImageData property.
- Save the updated Excel file using Workbook.Save(string) method.
The following code sample shows how to add image to a comment in Excel using C#.
// Instantiating a Workbook object | |
Workbook workbook = new Workbook("workbook.xlsx"); | |
// Getting a reference of comments collection with the first sheet | |
CommentCollection comments = workbook.Worksheets[0].Comments; | |
// Adding a comment to cell A1 | |
int commentIndex = comments.Add(0, 0); | |
Comment comment = comments[commentIndex]; | |
comment.Note = "First note."; | |
comment.Font.Name = "Times New Roman"; | |
// Loading an image into stream | |
Bitmap bmp = new Bitmap("logo.jpg"); | |
MemoryStream ms = new MemoryStream(); | |
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); | |
// Setting image data to the shape associated with the comment | |
comment.CommentShape.Fill.ImageData = ms.ToArray(); | |
// Saving the workbook | |
workbook.Save("book1.xlsx"); |
Apply Formatting to Comments in Excel using C#
The following are the steps to apply formatting to the comments in Excel using C#.
- Load the Excel file using Workbook class.
- Get reference of the desired Worksheet from Workbook.Worksheets collection.
- Add comment to the worksheet using Worksheet.Comments.Add(string) method and get the reference of the comment in a Comment object.
- Set comment’s note using Comment.Note property.
- Set the desired formatting options of the comment.
- Save the updated Excel file using Workbook.Save(string) method.
The following code sample shows how to set formatting of the comments in Excel using C#.
// Instantiating a Workbook object | |
Workbook workbook = new Workbook("workbook.xlsx"); | |
// Obtaining the reference of the first worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding a comment to "F5" cell | |
int commentIndex = worksheet.Comments.Add("F5"); | |
// Accessing the newly added comment | |
Comment comment = worksheet.Comments[commentIndex]; | |
// Setting the comment note | |
comment.Note = "Hello Aspose!"; | |
// Setting the font size of a comment to 14 | |
comment.Font.Size = 14; | |
// Setting the font of a comment to bold | |
comment.Font.IsBold = true; | |
// Setting the height of the font to 10 | |
comment.HeightCM = 10; | |
// Setting the width of the font to 2 | |
comment.WidthCM = 2; | |
// Saving the Excel file | |
workbook.Save("output.xlsx"); |
Get a Free License
You can use Aspose.Cells for .NET without evaluation limitations using a temporary license.
Conclusion
In this article, you have learned how to add comments to Excel worksheets using C#. Furthermore, you have seen how to add image to a comment programmatically. You can easily integrate the provided code within your .NET, .NET Core, or Xamarin based applications. In addition, you can explore the documentation of Aspose.Cells for .NET. In case you would have any queries, feel free to post to our forum.