DsExcel .NET enables you to annotate a worksheet by writing comments on cells in order to specify additional information about the data it contains.
For instance, let us assume you want to enter only the numeric information in an individual cell of a worksheet. To accomplish this, instead of populating a small cell with large notes, it is more ideal to use a short comment (something like "Please enter only numeric characters in this cell") in order to provide additional context for the data represented in that cell.
The cells annotated with comments will display a small red indicator (at the corner of the cell) which appear when your mouse pointer is placed on that particular cell. The text in the comments can be edited, copied and formatted. Also, the comments can be moved, resized or deleted, can be made hidden or visible and their indicators can also be customized as per your preferences.
The following tasks can be performed while applying comments in cells of a spreadsheet:
In DsExcel .NET, a cell comment instance is represented by the IComment interface. You can insert comment to a cell or a range of cells using the AddComment method of the IRange interface. You can also set the text of the comment using the Text property of the IComment interface.
Refer to the following example code to add comment to a cell.
C# |
Copy Code |
---|---|
// Create a comment for the range ["C3"] IComment commentC3 = worksheet.Range["C3"].AddComment("Range C3's comment."); //Change the text of the comment. commentC3.Text = "Range C3's new comment."; |
You can configure the layout of the comment added to an individual cell or a range of cells using Shape property of the IComment interface.
Refer to the following example code to set comment layout.
C# |
Copy Code |
---|---|
//Configure comment layout commentC3.Shape.Line.Color.RGB = Color.Green; commentC3.Shape.Line.Weight = 7; commentC3.Shape.Fill.Color.RGB = Color.Gray; commentC3.Shape.Width = 100; commentC3.Shape.Height = 200; commentC3.Shape.TextFrame.TextRange.Font.Bold = true; commentC3.Visible = true; |
You can choose to keep comments hidden or visible by using the Visible property of the IComment interface.
Refer to the following example code to show/hide comment added to a cell.
C# |
Copy Code |
---|---|
//Show Comment worksheet.Range["C3"].Comment.Visible = true; //Hide Comment worksheet.Range["C3"].Comment.Visible = false; |
You can represent the author of the comment by using the Author property of the IComment interface. Also, you can use this property to change the author of an existing comment.
Refer to the following example code to set comment author for a cell.
C# |
Copy Code |
---|---|
// Set comment author workbook.Author = "joneshan"; worksheet.Range["H6"].AddComment("H6's comment."); //H6's comment author is "joneshan". var authorH6 = worksheet.Range["H6"].Comment.Author; |
You can set the rich text for the comment using the properties and methods of the ITextFrame Interface that control the text style.
Refer to the following example code to set rich text for the comment.
C# |
Copy Code |
---|---|
IComment commentC3 = worksheet.Range["C3"].AddComment("Cell "); // Set paragraph's font style commentC3.Shape.TextFrame.TextRange.Paragraphs[0].Font.Bold = true; // Add comment in paragraph commentC3.Shape.TextFrame.TextRange.Paragraphs[0].Runs.Add(" C3"); commentC3.Shape.TextFrame.TextRange.Paragraphs[0].Runs.Add(" Comment"); commentC3.Shape.TextFrame.TextRange.Paragraphs[0].Runs.Add(" Added"); // Set the style of the second comment commentC3.Shape.TextFrame.TextRange.Paragraphs[0].Runs[2].Font.Italic = true; commentC3.Shape.TextFrame.TextRange.Paragraphs[0].Runs[2].Font.Bold = true; // Show comment commentC3.Visible = true; |
You can delete the comment added to a cell or to a cell range using the Delete method of the IComment interface and the IRange interface respectively.
Refer to the following example code to delete comment from a cell.
C# |
Copy Code |
---|---|
// Delete Comment instance
commentC3.Delete(); |