Comment is text about criticism or annotation. It is often written on the blank when reading one book, which can help reader master contents.
At present, we often write comments in electronic documents. For example, if there is some special data or important data in an Excel workhsheet, we write comment for it to tell others what it shows. So, how to write comment in Excel?
Then, I will introduce one method to write comment in Excel with C#. This method is based on a C# .NET Excel component, Spire.XLS for .NET. Therefore, we need to download this compnent and install it.
1. Create a new project and name it, WriteComment.
2. Add Spire.XLS for .NET DLL file as reference. Because we will set format for cells, so we need to add another reference System.Drawing.
3. Create a new workbook and set column width. Then, write text which we want to add comment by rang.Text. Use range.Comment.Text to set regular comment and range.Comment.RichText.Text to set rich text comment.
Use the code:
static void Main(string args)
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets;
ExcelFont fontBlue = workbook.CreateFont();
fontBlue.KnownColor = ExcelColors.SkyBlue;
ExcelFont fontGreen = workbook.CreateFont();
fontGreen.KnownColor = ExcelColors.SeaGreen;
CellRange range = sheet.Range[“A1”];
range.Text = “Regular comment”;
sheet.Range[“A1”].Style.Font.Color = Color.DarkGreen;
sheet.Range[“A1”].Style.Font.FontName = “Segoe Script”;
sheet.Range[“A1”].Style.Font.Size = 12;
sheet.Range[“A1”].Style.Font.IsBold = true;
range.Comment.Text = “Regular comment”;
//Rich Text Comment
range = sheet.Range[“A3”];
range.Text = “Rich text comment”;
sheet.Range[“A3”].Style.Font.Color = Color.DarkBlue;
sheet.Range[“A3”].Style.Font.FontName = “Segoe Script”;
sheet.Range[“A3”].Style.Font.Size = 12;
sheet.Range[“A3”].Style.Font.IsBold = true;
//Set Rich Text Comment Format
range.Comment.RichText.Text = “Rich text comment”;
range.Comment.RichText.SetFont(0, 4, fontGreen);
range.Comment.RichText.SetFont(5, 9, fontBlue);
//Save and Launch