C#在Excel表格中插入、編輯和刪除批注
概述
為文檔添加必要的批注可以給文檔使用者提供重要的提示信息,下面的示例中,將介紹通過C#編程語(yǔ)言來給Excel表格中的指定單元格內(nèi)容添加批注,此外,對(duì)于已有的批注,如果需要修改,我們也可以進(jìn)行編輯或者刪除批注。示例內(nèi)容將包含以下主要內(nèi)容:
1.插入批注
1.1 插入文本
1.2 插入圖片
2.編輯批注
2.1 修改批注內(nèi)容
2.1 設(shè)置批注可見性
3.刪除批注
工具
提示:在進(jìn)行代碼操作之前,需下載安裝Spire.Xls,并添加引用dll文件,添加如下using指令
using System;
using Spire.Xls;
using System.Drawing;
代碼示例(供參考)
1.插入Excel批注
【C#】
步驟1:實(shí)例化一個(gè)Workbook類實(shí)例并加載Excel文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
步驟2:獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
步驟3:插入文本批注
string comment = "注意:\n 責(zé)任人兼設(shè)備維護(hù)人";//設(shè)置批注文本 ExcelFont font = workbook.CreateFont();//設(shè)置批注字體格式 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)
{
//實(shí)例化一個(gè)Workbook類實(shí)例并加載Excel文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile("test.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//設(shè)置批注文本
string comment = "注意:\n 責(zé)任人兼設(shè)備維護(hù)人";
//設(shè)置批注字體
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:創(chuàng)建一個(gè)Workbook類對(duì)象,并加載Excel文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile("AddComment.xlsx");
步驟2:獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
步驟3:修改工作表中的第一個(gè)批注
ExcelComment comment0 = workbook.Worksheets[0].Comments[0]; sheet.Comments[0].Text = "This is a new comment";
步驟4:設(shè)置批注可見性(隱藏、顯示)
//設(shè)置指定批注不可見(隱藏) sheet.Comments[0].IsVisible = true; //設(shè)置指定批注可見(顯示) 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)
{
//創(chuàng)建一個(gè)Workbook類對(duì)象,并加載Excel文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile("AddComment.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//修改工作表中的第一個(gè)批注
ExcelComment comment0 = workbook.Worksheets[0].Comments[0];
sheet.Comments[0].Text = "This is a new comment";
//設(shè)置指定批注不可見(隱藏)
sheet.Comments[0].IsVisible = true;
//設(shè)置指定批注可見(顯示)
sheet.Comments[1].IsVisible = false;
//保存并打開文檔
workbook.SaveToFile("ModifyComment.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("ModifyComment.xlsx");
}
}
}
3.刪除Excel批注
【C#】
//實(shí)例化Wordbook類實(shí)例并加載Excel文檔
Workbook workbook = new Workbook();
workbook.LoadFromFile("Comments.xlsx");
//獲取第一個(gè)工作表
Worksheet sheet = workbook.Worksheets[0];
//刪除工作表中的第2個(gè)批注
sheet.Comments[1].Remove();
//保存并打開文檔
workbook.SaveToFile("RemoveComment.xlsx", ExcelVersion.Version2013);
System.Diagnostics.Process.Start("RemoveComment.xlsx");
以上全部為本篇文章的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#?將Excel轉(zhuǎn)為PDF時(shí)自定義表格紙張大小的代碼思路
- c#使用EPPlus封裝excel表格導(dǎo)入功能的問題
- C#插入圖片到Excel表格單元格代碼詳解
- c# 將Datatable數(shù)據(jù)導(dǎo)出到Excel表格中
- c#中合并excel表格的方法示例
- C#基于COM方式讀取Excel表格的方法
- C#實(shí)現(xiàn)將DataTable內(nèi)容輸出到Excel表格的方法
- C#使用oledb讀取excel表格內(nèi)容到datatable的方法
- C#使用Ado.Net更新和添加數(shù)據(jù)到Excel表格的方法
- 基于NPOI用C#開發(fā)的Excel以及表格設(shè)置
相關(guān)文章
C#客戶端HttpClient請(qǐng)求認(rèn)證及數(shù)據(jù)傳輸
本文詳細(xì)講解了C#客戶端HttpClient請(qǐng)求認(rèn)證及數(shù)據(jù)傳輸,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
DevExpress實(shí)現(xiàn)GridView當(dāng)無數(shù)據(jù)行時(shí)提示消息
這篇文章主要介紹了DevExpress實(shí)現(xiàn)GridView當(dāng)無數(shù)據(jù)行時(shí)提示消息,需要的朋友可以參考下2014-08-08
C#?Windows?Forms中實(shí)現(xiàn)控件之間的連接線的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何在C#?Windows?Forms應(yīng)用程序中實(shí)現(xiàn)繪圖工具中多個(gè)控件之間的連接線功能,文中的示例代碼講解詳細(xì),需要的可以參考下2024-02-02
C# 實(shí)現(xiàn)ADSL自動(dòng)斷網(wǎng)和撥號(hào)的方法(適用于撥號(hào)用戶)
下面小編就為大家?guī)硪黄狢# 實(shí)現(xiàn)ADSL自動(dòng)斷網(wǎng)和撥號(hào)的方法(適用于撥號(hào)用戶)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
C#中GridView動(dòng)態(tài)添加列的實(shí)現(xiàn)方法
這篇文章主要介紹了C#中GridView動(dòng)態(tài)添加列的實(shí)現(xiàn)方法,涉及C#中GridView的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

