Add Comment with Author in Excel

As we know, when we add a comment in MS-Excel, there will be Author appended automatically by Excel program. In fact, there is not the Author property in the comment in MS-Excel, which is just a text with special bold font style, and you can edit, remove, and insert other strings. After adding a comment (e.g., Test comment1) by using Spire.XLS component, there is no author is written into excel file. Check the below picture:

How to add a comment with editable Author property by Spire.XLS component? Just like the comment in the following picture:

Download Spire.XLS for .NET and install it on your computer. Then create CreateComment(CellRange range,string text, string author=null) method to implement it. Firstly, add a comment for a specified range by calling the CellRange.AddComment() method. Then, if the author is specified, create a new font with bold style and apply the font to the author text.

The following is the code for the method:

[C#]
static ExcelComment CreateComment(CellRange range,string text, string author=null)
        {
           ExcelComment comment= range.AddComment();
           comment.Text = String.IsNullOrEmpty(author) ? text : author + ":\n" + text;
           if (!String.IsNullOrEmpty(author))
           {
               ExcelFont font = range.Worksheet.Workbook.CreateFont();
               font.FontName = "Tahoma";
               font.KnownColor = ExcelColors.Black;
               font.IsBold = true;
               comment.RichText.SetFont(0, author.Length,font);
           }
           return comment;
        }

[VB.NET]
Private Function CreateComment(ByVal range As CellRange, ByVal text As String, Optional ByVal author As String = Nothing) As ExcelComment
        Dim comment As ExcelComment = range.AddComment()
        comment.Text = If([String].IsNullOrEmpty(author), text, author & ":" & vbLf & text)
        If Not [String].IsNullOrEmpty(author) Then
            Dim font As ExcelFont = range.Worksheet.Workbook.CreateFont()
            font.FontName = "Tahoma"
            font.KnownColor = ExcelColors.Black
            font.IsBold = True
            comment.RichText.SetFont(0, author.Length, font)
        End If
        Return comment
    End Function

Next, you can call the method to add comments with author to your excel file.

[C#]
CellRange range = sheet.Range["A1"];
CreateComment(range, "Test comment2", "E-iceblue");
[VB.NET]
Dim range As CellRange = sheet.Range("A1")
CreateComment(range, "Test comment2", "E-iceblue")