C#在Excel表格中插入、编辑和删除批注

2020-01-05 09:25:41于丽


ExcelComment comment0 = workbook.Worksheets[0].Comments[0];
sheet.Comments[0].Text = "This is a new comment";

步骤4:设置批注可见性(隐藏、显示)


//设置指定批注不可见(隐藏)
sheet.Comments[0].IsVisible = true;
//设置指定批注可见(显示)
sheet.Comments[1].IsVisible = false;

步骤5:保存文档


workbook.SaveToFile("ModifyComment.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("ModifyComment.xlsx");

效果图:

C#,Excel,表格,批注

全部代码:


using System;
using Spire.Xls;
using System.Drawing;

namespace ModifyComment_XLS
{
 class Program
 {
  static void Main(string[] args)
  {
   //创建一个Workbook类对象,并加载Excel文档
   Workbook workbook = new Workbook();
   workbook.LoadFromFile("AddComment.xlsx");

   //获取第一个工作表
   Worksheet sheet = workbook.Worksheets[0];

   //修改工作表中的第一个批注   
   ExcelComment comment0 = workbook.Worksheets[0].Comments[0];
   sheet.Comments[0].Text = "This is a new comment";

   //设置指定批注不可见(隐藏)
   sheet.Comments[0].IsVisible = true;
   //设置指定批注可见(显示)
   sheet.Comments[1].IsVisible = false;

   //保存并打开文档
   workbook.SaveToFile("ModifyComment.xlsx", ExcelVersion.Version2013);
   System.Diagnostics.Process.Start("ModifyComment.xlsx");
  }
 }
}

3.删除Excel批注

【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");