Ask Your Question
1

What is the process to obtain a comment object from an Excel cell using C#?

asked 2022-01-10 11:00:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-07-04 07:00:00 +0000

nofretete gravatar image

To obtain a comment object from an Excel cell using C#, you can follow these steps:

  1. Open the Excel workbook using the Workbook class in the Microsoft.Office.Interop.Excel namespace.

  2. Select the worksheet in which the target cell is located using the Worksheet property of the Workbook object.

  3. Access the target cell using the Range property of the Worksheet object, and then use the Comment property of the Range object to get the comment attached to the cell.

  4. You can then read or modify the properties of the comment object (e.g. Comment.Text to get the comment text, Comment.Author to get the author of the comment, Comment.Visible to get or set the visibility of the comment, etc.).

Here is some sample code to illustrate the process:

using Microsoft.Office.Interop.Excel;

// Open the Excel workbook
Workbook workbook = new Application().Workbooks.Open(@"C:\example.xlsx");

// Select the worksheet
Worksheet worksheet = workbook.Worksheets["Sheet1"];

// Get the comment from cell A1
Range cellRange = worksheet.Range["A1"];
Comment comment = cellRange.Comment;

// Display the comment text and author
if (comment != null)
{
    Console.WriteLine("Comment text: " + comment.Text);
    Console.WriteLine("Comment author: " + comment.Author);
}
else
{
    Console.WriteLine("There is no comment in cell A1.");
}

// Modify the comment properties (e.g. set the visibility to false)
if (comment != null)
{
    comment.Visible = false;
}

// Close the workbook
workbook.Close();
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-01-10 11:00:00 +0000

Seen: 15 times

Last updated: Jul 04 '21