To obtain a comment object from an Excel cell using C#, you can follow these steps:
Open the Excel workbook using the Workbook
class in the Microsoft.Office.Interop.Excel
namespace.
Select the worksheet in which the target cell is located using the Worksheet
property of the Workbook
object.
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.
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();
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
Asked: 2022-01-10 11:00:00 +0000
Seen: 15 times
Last updated: Jul 04 '21
Can Jupyter Notebook be converted to HTML during its execution?
How can I transfer a formula from one worksheet to another by using a cell reference?
How can the final value of the FILTER function be retrieved in Google Sheets?
What is the method for copying the final pre-populated cell with autofill as demonstrated below?
Can the Excel cell entries be transformed into a uniform date and time format?
How do you create a table with a fixed element and two headers?
What is the process to deduct a single cell from each cell within a specific range?