Read, Add, and Edit Threaded Comments in Excel using C#

Threaded comments in Excel are an essential feature for collaborative work. It allows multiple users to add, edit, and review conversation threads within a shared document. In certain cases, we may need to manage these threaded comments programmatically. In this blog post, we will learn how to read, add, and edit threaded comments in Excel using C#.

This article covers the following topics:

  1. C# Excel API for Managing Threaded Comments
  2. Add Threaded Comments in Excel using C#
  3. Read Threaded Comments for a Specific Cell in Excel
  4. Read All Threaded Comments in Excel using C#
  5. Edit a Threaded Comment in Excel using C#
  6. Delete Threaded Comments in Excel using C#
  7. Free Resources

C# Excel API for Managing Threaded Comments

For working with threaded comments in Excel worksheets, we will use Aspose.Cells for .NET API. It is a powerful spreadsheet programming API that enables developers to create, manipulate, and convert Excel files in .NET applications. By leveraging Aspose.Cells for .NET, we can easily add, read, edit, or delete threaded comments in Excel worksheets.

To utilize the API, please either download the DLL or install it from NuGet with the following command:

PM> Install-Package Aspose.Cells 

Add a Threaded Comment in Excel using C#

We can easily add threaded comments to an Excel worksheet by following the steps below:

  1. Create an instance of the Workbook class.
  2. Access the specific worksheet by its index.
  3. Add an author to the ThreadedCommentAuthors collection using the Add() method.
  4. Get the ThreadedCommentAuthor class object for the newly created author by its index.
  5. Add the threaded comment using the AddThreadedComment() method. It takes the cell name, comment text, and ThreadedCommentAuthor object as arguments.
  6. Save the Excel file using the Workbook.Save(string) method.

The following code sample shows how to add a threaded comment to an Excel worksheet using C#.

// This code example demonstrates how to add threaded comments in an Excel worksheet
// Create an instance of the Workbook class
Workbook workbook = new Workbook();
// Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Add an Author
var authorIndex = workbook.Worksheets.ThreadedCommentAuthors.Add("Aspose Test", "", "");
var author = workbook.Worksheets.ThreadedCommentAuthors[authorIndex];
// Add Threaded Comment
worksheet.Comments.AddThreadedComment("A1", "Test Threaded Comment", author);
// Save the output file
workbook.Save("AddThreadedComments_out.xlsx");

Read Threaded Comments for a Specific Cell in Excel

To read threaded comments from a specified cell in an Excel worksheet, follow these steps:

  1. Load an existing Excel file using the Workbook class.
  2. Access the worksheet by its index.
  3. Get threaded comments for a specific cell using the GetThreadedComments() method. It takes the cell name as an argument.
  4. Loop through all the threaded comments and read the details.

The following code sample shows how to read threaded comments for the specified column from an Excel worksheet using C#.

// This code example demonstrates how to read threaded comments for a specified cell in an Excel worksheet
// Load an existing Excel file
Workbook workbook = new Workbook("AddThreadedComments_out.xlsx");
// Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Get Threaded Comments for a specific cell
var threadedComments = worksheet.Comments.GetThreadedComments("A1");
// Read the threaded comments
foreach (var comment in threadedComments)
{
Console.WriteLine("Author Name: " + comment.Author.Name);
Console.WriteLine("Threaded comment Notes:" + comment.Notes);
}
Author Name: author@domain.com
Threaded comment Notes:Test Threaded Comment

Read All Threaded Comments in Excel using C#

Similarly, we can read all the threaded comments available in an Excel worksheet by following the steps below:

  1. Load an existing Excel file using the Workbook class.
  2. Loop through all the comments and read threaded comments one by one.

The following code sample shows how to read all the threaded comments from an Excel worksheet using C#.

// This code example demonstrates how to read threaded comments for a specified cell in an Excel worksheet
// Load an existing Excel file
Workbook workbook = new Workbook("AddThreadedComments_out.xlsx");
// Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Get all the comments
var comments = worksheet.Comments;
// Read all the threaded comments
foreach (var comment in comments)
{
// Process threaded comments
foreach (var threadedComment in comment.ThreadedComments)
{
Console.WriteLine("Author Name: " + threadedComment.Author.Name);
Console.WriteLine("Threaded comment author User Id: " + threadedComment.Author.UserId);
Console.WriteLine("Threaded comment author ProviderId:" + threadedComment.Author.ProviderId);
Console.WriteLine("Threaded comment Notes:" + threadedComment.Notes);
}
}

Edit a Threaded Comment in Excel using C#

Please follow the steps below in order to update any of the threaded comments in the Excel:

  1. Load an existing Excel file using the Workbook class.
  2. Access the worksheet by its index.
  3. Get threaded comments for a specific cell using the GetThreadedComments() method. It takes the cell name as an argument.
  4. Update the comment’s notes property.
  5. Save the Excel file using the Workbook.Save(string) method.

The following code sample shows how to edit threaded comments in an Excel worksheet using C#.

// This code example demonstrates how to edit threaded comments in an Excel worksheet
// Load an existing Excel file
Workbook workbook = new Workbook("AddThreadedComments_out.xlsx");
// Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Get Threaded Comments for a specific cell
var threadedComments = worksheet.Comments.GetThreadedComments("A1");
var comment = threadedComments[0];
// Update the comment note
comment.Notes = "Updated Comment";
// Save the output file
workbook.Save("EditThreadedComments.xlsx");

Delete Threaded Comments in Excel using C#

We can also delete the threaded comments for a specific cell in an Excel worksheet by following the steps below:

  1. Load an existing Excel file using the Workbook class.
  2. Access the worksheet by its index.
  3. Remove a comment from the comments collection using the RemoveAt() method. It takes the cell name as an argument.
  4. Save the Excel file using the Save(string) method.

The following code sample shows how to delete threaded comments in an Excel worksheet using C#.

// This code example demonstrates how to delete threaded comments in an Excel worksheet
// Load an existing Excel file
Workbook workbook = new Workbook("AddThreadedComments_out.xlsx");
// Get all the comments
var comments = workbook.Worksheets[0].Comments;
// Remove Comments
comments.RemoveAt("A1");
// Save the output file
workbook.Save("DeleteThreadedComments.xlsx");

Get a Free License

Please get a free temporary license to read, add, and edit threaded comments in Excel without trial restrictions. Visit our ‘Temporary License’ page for easy-to-follow instructions on claiming your free license quickly.

Threaded Comments in Excel – Free Resources

In addition to working with threaded comments in Excel worksheets, you can learn more about creating, manipulating, and converting Excel files. Explore various other features of the Aspose.Cells for .NET using the resources below:

Conclusion

In this article, we have learned how to read, add, edit, and delete threaded comments from Excel worksheets using C#. By leveraging Aspose.Cell for .NET, you can easily manipulate Excel worksheets in your C# applications. In case of any ambiguity, please contact us on our free support forum.

See Also