Overview
Adding the necessary annotations to the document provides important hints to the document consumer, and in the following example, the C # programming language is used to annotate the specified cell content in an Excel table, and for existing annotations, we can edit or delete annotations if necessary. The sample content will contain the following main content:
1. Inserting annotations
1.1 Inserting text
1.2 Inserting pictures
2. Editing annotations
2.1 Modifying annotation content
2.1 Setting Annotation Visibility
3. Delete annotations
Tools
Tip: Before you perform the code operation, download the installation Spire.xls and add the reference DLL file, adding the following using directive
Using System;
Using Spire.xls;
Using System.Drawing;
code example (for reference) 1. Inserting an Excel annotation
"C #"
Step 1: Instantiate an instance of the workbook class and load the Excel document
Workbook workbook = new Workbook();workbook.LoadFromFile("test.xlsx");
Step 2: Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
Step 3: Insert a text annotation
string comment = "注意:\n 责任人兼设备维护人";//设置批注文本ExcelFont font = workbook.CreateFont();//设置批注字体格式font.FontName = "Calibri";font.Color = Color.Black;font.IsBold = true;CellRange range = sheet.Range["I3"];//添加批注到指定单元格range.Comment.RichText.Text = comment;range.Comment.Width = 200;range.Comment.Height = 50;range.Comment.RichText.SetFont(10, 10, font);
Step 4: Insert a picture annotation
//加载图片,将图片插入到指定单元格的批注Image image = Image.FromFile("logo.png");sheet.Range["B2"].Comment.Fill.CustomPicture(image, "logo.png");sheet.Range["B2"].Comment.Height = image.Height;sheet.Range["B2"].Comment.Width = image.Width;
Step 5: Save the document
workbook.SaveToFile("AddComment.xlsx", ExcelVersion.Version2013);System.Diagnostics.Process.Start("AddComment.xlsx");
Annotation Insert effect (for example):
2. Edit and modify Excel annotations
"C #"
Step 1: Create a Workbook class object and load the Excel document
Workbook workbook = new Workbook();workbook.LoadFromFile("AddComment.xlsx");
Step 2: Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
Step 3: Modify the first annotation in a worksheet
ExcelComment comment0 = workbook.Worksheets[0].Comments[0];sheet.Comments[0].Text = "This is a new comment";
Step 4: Set the visibility of annotations (hide, show)
//设置指定批注不可见(隐藏)sheet.Comments[0].IsVisible = true;//设置指定批注可见(显示)sheet.Comments[1].IsVisible = false;
Step 5: Save the document
workbook.SaveToFile("ModifyComment.xlsx", ExcelVersion.Version2013);System.Diagnostics.Process.Start("ModifyComment.xlsx");
:
3. Delete Excel annotations
"C #"
//实例化Wordbook类实例并加载Excel文档Workbook workbook = new Workbook();workbook.LoadFromFile("Comments.xlsx");//获取第一个工作表Worksheet sheet = workbook.Worksheets[0];//删除工作表中的第2个批注sheet.Comments[1].Remove();//保存并打开文档workbook.SaveToFile("RemoveComment.xlsx", ExcelVersion.Version2013);System.Diagnostics.Process.Start("RemoveComment.xlsx");
All of the above are the entire contents of this article.
Please specify the source if you want to reprint.
< end of this article >
How C # Inserts, edits, and deletes annotations in an Excel table