Canalblog
Editer l'article Suivre ce blog Administration + Créer mon blog
Publicité
SpireXLS
29 mars 2012

Use C#/VB.NET to Add Comment for Excel Cells

As is known, comment is used to show readers' thoughts or suggestions about one article or one book. Readers can give comment for specified words or sentences or show feeling about whole article or book.

Actually, not only article or book, MS Excel provides users with a function to add comment for data information. Difference from Word, Excel Comment is added for cells. For example, if we find that data information of one row header cell is not correct or has been changed, we can add comment with correct or changed information.

So, this article will focus on how to add comment in Excel by using C# and VB.NET.

Note: Spire.XLS is used in this method. Please download Spire.XLS and install it firstly, and then add its dll file as reference in your project if you want to use the following code.

STEPS:

1. Load document which I will add comment firstly and initialize worksheet.

C#

Workbook workbook = new Workbook();

workbook.LoadFromFile(@"E:\work\Documents\Customers.xlsx");

Worksheet sheet = workbook.Worksheets[0];

VB.NET

Dim workbook As New Workbook()

workbook.LoadFromFile("E:\work\Documents\Customers.xlsx")

Dim sheet As Worksheet = workbook.Worksheets(0)

2. Declare a string which shows comment contents. Then create a font style to be convenient for using in next step.

C#

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;

VB.NET

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

3. Add comment for cell. Firstly, Assign value as string I declare in step 2 to sheet.Range["Cell"].Comment.RichText.Text. Then, set comment width and height. Finally, use sheet.Range["Cell"].Comment.RichText.SetFont() method. Three parameters should be passed to this method, start position, end position and font which I have been declared in step 2.

C#

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

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)

4. Save file as a new name and launch this file.

C#

workbook.SaveToFile("Properties.xlsx", ExcelVersion.Version2010); 

System.Diagnostics.Process.Start("Properties.xlsx");

VB.NET

workbook.SaveToFile("Properties.xlsx", ExcelVersion.Version2010)

System.Diagnostics.Process.Start("Properties.xlsx")

Now, you can get result as following:

___________________________________________________________________________________________________

Spire.XLS, a third-party component, is very powerful on operating Excel files, including to create, open, modify, insert external content, convert between Excel and other popular file format, such as HTML, XML, PDF etc. Click here to Learn more.

Related Post:

Write Word Comments

 

Publicité
Publicité
Commentaires
SpireXLS
Publicité
Publicité