Add formatted comments to Excel in WPF

Excel comments in individual cells are extra information that explain more about the data in these cells. The information can be notes for readers, reminders for yourself, and cross-references to other reports. In this article, I am going to introduce how to add and format Excel comments using Spire.XLS for WPF.

Before start, please download Spire.XLS Pack and add the Spire.XLS.Wpf.dll and Spire.License.dll from Bin folder to reference of your WPF project.

Code Snippets:

Step 1: Initialize a new Workbook, get the first worksheet from workbook.

Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];

Step 2: Create a font style that will be used to format comment.

ExcelFont font = wb.CreateFont();
font.FontName = "Calibri";
font.Color = Color.Blue;
font.Size = 10;
font.IsBold = true;

Step 3: Add a comment to C4 and set the size of comment box, set the text and set font for specified range of characters.

ExcelComment comment = sheet.Range["C4"].Comment;
comment.Height = 80;
comment.Width = 200;
comment.RichText.Text = "This comment is made by Spire.XLS for WPF.";
comment.RichText.SetFont(23, 40, font);

Step 4: Save and launch the file.

wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("result.xlsx");

Output:

Add formatted comments to Excel in WPF

Full Code:

[C#]
using Spire.Xls;
using System.Drawing;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Workbook wb = new Workbook();
            Worksheet sheet = wb.Worksheets[0];

            ExcelFont font = wb.CreateFont();
            font.FontName = "Calibri";
            font.Color = Color.Blue;
            font.Size = 10;
            font.IsBold = true;

            ExcelComment comment = sheet.Range["C4"].Comment;
            comment.Height = 80;
            comment.Width = 200;
            comment.RichText.Text = "This comment is made by Spire.XLS for WPF.";
            comment.RichText.SetFont(23, 40, font);

            wb.SaveToFile("result.xlsx", ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("result.xlsx");

        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports System.Drawing
Imports System.Windows

Namespace WpfApplication1
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub
		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			Dim wb As New Workbook()
			Dim sheet As Worksheet = wb.Worksheets(0)

			Dim font As ExcelFont = wb.CreateFont()
			font.FontName = "Calibri"
			font.Color = Color.Blue
			font.Size = 10
			font.IsBold = True

			Dim comment As ExcelComment = sheet.Range("C4").Comment
			comment.Height = 80
			comment.Width = 200
			comment.RichText.Text = "This comment is made by Spire.XLS for WPF."
			comment.RichText.SetFont(23, 40, font)

			wb.SaveToFile("result.xlsx", ExcelVersion.Version2013)
			System.Diagnostics.Process.Start("result.xlsx")

		End Sub
	End Class
End Namespace