C#/VB.NET: Add or Format Comments in Excel

In Excel, comments are used to explain the contents in cells or to add additional information that might be useful to readers. Using Spire.XLS for .NET, we can add comments to Excel cells easily as well as customizing the appearance by setting the size of the comment box or applying a font style to the comment text.  In this article, we will demonstrate how to add comments to Excel worksheets programmatically in C#/VB.NET from the following two parts.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Add Comments in an Excel Worksheet

Spire.XLS offers the CellRange.AddComment() method to add the regular text comment to Excel worksheet. The following are the steps.

  • Initialize an instance of Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the first worksheet of the Excel file using Workbook.Worksheets[int] property.
  • Add a comment in a specific cell range using CellRange.AddComment() method and then set the comment text through the Text property of the ExcelComment object.
  • Save the document to another file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace WordDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook workbook = new Workbook();

            //Load the sample workbook
            workbook.LoadFromFile("Sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Add regular comment to specific cell range C6
            CellRange range = sheet.Range["C6"];
            ExcelComment comment = range.AddComment();
            comment.Text = "Regular comment";                      
            
            //Save the Excel workbook.
            workbook.SaveToFile("Addcomment.xlsx", ExcelVersion.Version2016);

        }
}

C#/VB.NET: Add or Format Comments in Excel

Apply Formatting to Comments in an Excel Worksheet

Spire.XLS offers the Comment.RichText.SetFont() method to apply font formatting for comments in Excel worksheets.

  • Initialize an instance of Workbook class.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Get the first worksheet of the Excel file using Workbook.Worksheets[int] property.
  • Add a comment in a specific cell range using CellRange.AddComment() method and then set the comment text.
  • Create an ExcelFont object and apply the font to the comment text using ExcelComment.RichText.SetFont() method.
  • Save the document to another file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace ExcelComment
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook workbook = new Workbook();

            //Load the sample workbook
            workbook.LoadFromFile("Sample.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Add comment to specific cell range C6                                              
            CellRange range = sheet.Range["C6"];
            ExcelComment comment = range.AddComment();
            comment.Text = "Comment with format";                      
            
            //Set the width and height
            comment.Width = 100;
            comment.Height = 200;

            //Display the comment
            comment.Visible = true;

            //Create a font
            ExcelFont font = workbook.CreateFont();
            font.FontName = "Calibri";
            font.Size = 14;
            font.KnownColor = ExcelColors.LightBlue;
            font.IsBold = true;

            //Apply the font to the comment text
            comment.RichText.SetFont(0,27,font);

            //Save the Excel workbook.
            workbook.SaveToFile("AddcommentwithFormat.xlsx", ExcelVersion.Version2016);

        }
    }
}

C#/VB.NET: Add or Format Comments in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.