DEV Community

Cover image for How to Add Comments to Excel Documents Using C#
Jollen Moyani for Syncfusion, Inc.

Posted on • Originally published at syncfusion.com on

How to Add Comments to Excel Documents Using C#

Commenting is a useful feature in Excel documents that allows users to add additional information about specific cell data, calculations, and references used inside a cell. Comments serve as a form of documentation to help you and others understand the content and context of your Excel workbook. They are particularly useful when sharing workbooks with other collaborators.

From the 2023 Volume 3 release on, the Syncfusion Excel Library (XlsIO) supports adding threaded comments to Excel documents. With this feature, you can allow multiple users to add comments or reply to existing comments in an Excel worksheet.

You can perform the following actions with threaded-comments support in the Excel Library:

Let’s see how to perform these actions with code examples!

Note: If you are new to our Excel Library, following our getting started guide is highly recommended.

Add comment

A comment can be any text related to the cell information. The following code example illustrates how to add threaded comments to a cell in a worksheet.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;

    FileStream inputStream = new FileStream("../../../Data/CommentsTemplate.xlsx", FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);

    IWorksheet worksheet = workbook.Worksheets[0];

    //Add threaded comments.
    IThreadedComment threadedComment = worksheet.Range["H16"].AddThreadedComment("What is the reason for the higher total amount of \"desk\" in the west region?", "User1", DateTime.Now);

    FileStream stream = new FileStream("output.xlsx", FileMode.Create, FileAccess.ReadWrite);
    workbook.SaveAs(stream);
    workbook.Close();
    excelEngine.Dispose();
}
Enter fullscreen mode Exit fullscreen mode

This results in the following image.

Adding a threaded comment in an Excel document

Adding a threaded comment in an Excel document

Reply to comment

You can add a reply to an existing comment in an Excel document. The following code example illustrates how to add a reply to a comment using C#

//Add threaded comments.
IThreadedComment threadedComment = worksheet.Range["H16"].AddThreadedComment("What is the reason for the higher total amount of \"desk\" in the west region?", "User1", DateTime.Now);

//Add reply to the threaded comment.
threadedComment.AddReply("The unit cost of desk is higher compared to other items in the west region. As a result, the total amount is elevated.", "User2", DateTime.Now);
Enter fullscreen mode Exit fullscreen mode

Refer to the following image.

Replying to a comment in an Excel document

Replying to a comment in an Excel document

Mark as resolved

You can also mark a discussion (comments) created for a cell as resolved. This way, other users know to ignore the discussion.

The following code example illustrates how to mark a discussion as resolved.

//Add threaded comments.
IThreadedComment threadedComment = worksheet.Range["H16"].AddThreadedComment("What is the reason for the higher total amount of \"desk\" in the west region?", "User1", DateTime.Now);

//Add reply to the threaded comment.
threadedComment.AddReply("The unit cost of desk is higher compared to other items in the west region. As a result, the total amount is elevated.", "User2", DateTime.Now);

//Mark as resolved.
threadedComment.IsResolved = true;
Enter fullscreen mode Exit fullscreen mode

Refer to the following image.

Marking a discussion as resolved in an Excel document

Marking a discussion as resolved in an Excel document

GitHub reference

You can download examples of threaded comments in Excel documents using C# from this GitHub page.

Conclusion

Thanks for reading! In this blog, we’ve explored the new threaded comments feature added to the Syncfusion Excel Library (XlsIO) for the 2023 Volume 3 release. This feature is useful for collaborating on Excel documents and getting feedback from others.

Take a moment to peruse the documentation, where you’ll find other Excel options and features like conditional formatting, tables, pivot tables, and charts.

Using the Excel Library, you can export Excel data to PDF, image, data table, CSV, TSV, HTML, collections of objects, ODS, JSON, and more file formats.

Are you already a Syncfusion user? You can download the product setup here. If you’re not a Syncfusion user yet, you can download a free 30-day trial here.

Please let us know in the comments section below if you have any questions about these features. You can also contact us through our support forum, support portal, or feedback portal. We are always happy to assist you!

Related blogs

Top comments (0)