How to Write Comments for Excel Cells with C#

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:

using System.Drawing;

using Spire.Xls;

 

namespace WriteComment

{

    class Comment

    {

        static void Main(string[] args)

        {

            Workbook workbook = new Workbook();

 

            //Initialize worksheet

            workbook.CreateEmptySheets(1);

            Worksheet sheet = workbook.Worksheets[0];

            sheet.SetColumnWidth(1, 25);

 

            //Create Font

            ExcelFont fontBlue = workbook.CreateFont();

            fontBlue.KnownColor = ExcelColors.SkyBlue;

            ExcelFont fontGreen = workbook.CreateFont();

            fontGreen.KnownColor = ExcelColors.SeaGreen;

 

            //Regular Comment

            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

            workbook.SaveToFile(“Write Comment.xls”);

            System.Diagnostics.Process.Start(workbook.FileName);

        }

    }

}

RESULT:

DOWNLOAD Spire.XLS for .NET Here

Advertisements

14 thoughts on “How to Write Comments for Excel Cells with C#

  1. Arianne Trish says:

    Thanks on your marvelous posting! I quite enjoyed reading it, you can be a great author.I will ensure that I bookmark your blog and definitely will come back in the foreseeable future. I want to encourage you continue your great work, have a nice evening! I absolutely love your blog and find most of your post’s to be just what I’m looking for. Does one offer guest writers to write content for you personally? I wouldn’t mind composing a post or elaborating on most of the subjects you write in relation to here. Again, awesome web site!

  2. SEO says:

    Hey! I simply wish to give an enormous thumbs
    up for the great info you might have here on this post.
    I will likely be coming back to your weblog for extra soon.

  3. http://www.kiwibox.com/ says:

    I’ve been surfing online more than 3 hours today, yet I never
    found any interesting article like yours. It’s pretty worth enough for me.

    In my view, if all webmasters and bloggers made good
    content as you did, the internet will be much more
    useful than ever before.

  4. Nigel says:

    I like the valuable information you provide in your
    articles. I will bookmark your weblog and check again here regularly.

    I’m quite sure I’ll learn a lot of new stuff right here!
    Best of luck for the next!

  5. lightweight smart shell me301t says:

    Hi would you mind sharing which blog platform you’re working with?
    I’m planning to start my own blog soon but I’m having a hard time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design seems different then most
    blogs and I’m looking for something unique. P.S
    My apologies for being off-topic but I had to ask!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s