4/26/2012

Add Excel Comment with C#, VB.NET

Everything is changing. So is the data information. When changes happen in excel, Comment provides great convenience for people to make notes about what has been changed. Besides, the old data information also can be kept. For example, your customer has changed his/her e-mail or contact, while you need use comment function to remind the author modify the information in time.

So how to make comment in excel with C#, VB.NET is the main point in this article. The whole procedure can be very easy since I use an excel creation component Spire.XLS to help me. You also can Freely Install Spire.XLS on the system.


How to add Excel comment with C#, VB.NET
Using Spire.XLS, I only need three steps to realize this task in a quick way. Please look at the below procedure.


Step1. Load an Excel file from System.

C# Code:
           //Load File
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"D:\michelle\e-iceblue\Spire.XLS\Demos\Data\comment.xlsx");
            Worksheet sheet = workbook.Worksheets[0];

VB.NET Code:
                          'Load File
                          Dim workbook As New Workbook()
                          workbook.LoadFromFile("D:\michelle\e-iceblue\Spire.XLS\Demos\Data\comment.xlsx")
                          Dim sheet As Worksheet = workbook.Worksheets(0)



Step2. Add comment in Excel.

C# Code:
            //Add Comment
            string str = "E-mail has been changed as c.white2304@gmail.com";
            ExcelFont font1 = workbook.CreateFont();
            font1.FontName = "Century Gothic";
            font1.Color = Color.DarkCyan;
            font1.IsBold = true;
            sheet.Range["A6"].Comment.RichText.Text = str;
            sheet.Range["A6"].Comment.Width = 200;
            sheet.Range["A6"].Comment.Height = 50;
            sheet.Range["A6"].Comment.RichText.SetFont(26, 47, font1);

VB.NET Code:
                          'Add Comment
                          Dim str As String = "E-mail has been changed as c.white2304@gmail.com"
                          Dim font1 As ExcelFont = workbook.CreateFont()
                          font1.FontName = "Century Gothic"
                          font1.Color = Color.DarkCyan
                          font1.IsBold = True

                          sheet.Range("A6").Comment.RichText.Text = str
                          sheet.Range("A6").Comment.Width = 200
                          sheet.Range("A6").Comment.Height = 50
                          sheet.Range("A6").Comment.RichText.SetFont(26, 47, font1)



Step3. Save and Launch the file.
C# Code:
            //Save and Launch
            workbook.SaveToFile("Comment.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("Comment.xlsx");

VB.NET Code:
                          'Save and Launch
                          workbook.SaveToFile("Comment.xlsx", ExcelVersion.Version2010)
                          System.Diagnostics.Process.Start("Comment.xlsx")

Note: 1. Please choose .NET Framework4 as the Target framework.

            2. Add Spire.XLS DLL and System.Drawing as references.

            3. Add the following using at the top of the file.
C#
using System.Drawing;
using Spire.Xls;
using Spire.Xls.Core;


Preview


There are many other Excel functions that can be realized by C#, VB.NET. So if you want to consult more, please see below:

2 comments:

  1. ohoooo thanks for sharing this useful information to us.
    Please visit here for get corporate training in some skills like excel , power point and so more.

    ReplyDelete
  2. online coding classes for kids Awesome article, it was exceptionally helpful! I simply began in this and I'm becoming more acquainted with it better! Cheers, keep doing awesome!

    ReplyDelete