国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#在Excel表格中插入、編輯和刪除批注

2020-01-24 00:17:23
字體:
來源:轉載
供稿:網友

概述

為文檔添加必要的批注可以給文檔使用者提供重要的提示信息,下面的示例中,將介紹通過C#編程語言來給Excel表格中的指定單元格內容添加批注,此外,對于已有的批注,如果需要修改,我們也可以進行編輯或者刪除批注。示例內容將包含以下主要內容:

1.插入批注

  1.1 插入文本

  1.2 插入圖片

2.編輯批注

 2.1 修改批注內容

 2.1 設置批注可見性

3.刪除批注

工具

 Spire.XLS for .NET 8.0 

提示:在進行代碼操作之前,需下載安裝Spire.Xls,并添加引用dll文件,添加如下using指令

using System;

using Spire.Xls;

using System.Drawing;

代碼示例(供參考)

1.插入Excel批注

【C#】

步驟1:實例化一個Workbook類實例并加載Excel文檔

Workbook workbook = new Workbook();workbook.LoadFromFile("test.xlsx");

步驟2:獲取第一個工作表

Worksheet sheet = workbook.Worksheets[0];

步驟3:插入文本批注

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

步驟4:插入圖片批注

//加載圖片,將圖片插入到指定單元格的批注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;

步驟5:保存文檔

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

批注插入效果(如下圖):

全部代碼:

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("test.xlsx");   //獲取第一個工作表   Worksheet sheet = workbook.Worksheets[0];   //設置批注文本   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);   //加載圖片,將圖片插入到指定單元格的批注   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;   //保存并打開文檔   workbook.SaveToFile("AddComment.xlsx", ExcelVersion.Version2013);   System.Diagnostics.Process.Start("AddComment.xlsx");  } }}

2. 修改、隱藏Excel批注

【C#】

步驟1:創建一個Workbook類對象,并加載Excel文檔

Workbook workbook = new Workbook();workbook.LoadFromFile("AddComment.xlsx");

步驟2:獲取第一個工作表

Worksheet sheet = workbook.Worksheets[0];

步驟3:修改工作表中的第一個批注        

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

效果圖:

全部代碼:

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

以上全部為本篇文章的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 尼勒克县| 本溪| 桃江县| 囊谦县| 界首市| 马尔康县| 西峡县| 张家口市| 泾源县| 连南| 宣威市| 九龙县| 五寨县| 涞源县| 佛坪县| 竹溪县| 闽侯县| 寿阳县| 浦城县| 青神县| 桐城市| 锦屏县| 灵台县| 乐安县| 山东省| 富宁县| 漳州市| 伊金霍洛旗| 昭通市| 绥江县| 资兴市| 金湖县| 建瓯市| 乌拉特前旗| 衡阳县| 定州市| 泰来县| 荣成市| 金溪县| 临江市| 蓬安县|