這篇文章將為大家詳細講解有關C#在Excel表格中如何實現(xiàn)插入、編輯和刪除批注,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
義烏ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!概述
為文檔添加必要的批注可以給文檔使用者提供重要的提示信息,下面的示例中,將介紹通過C#編程語言來給Excel表格中的指定單元格內(nèi)容添加批注,此外,對于已有的批注,如果需要修改,我們也可以進行編輯或者刪除批注。示例內(nèi)容將包含以下主要內(nèi)容:
1.插入批注
1.1 插入文本
1.2 插入圖片
2.編輯批注
2.1 修改批注內(nèi)容
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:創(chuàng)建一個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)
{
//創(chuàng)建一個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");關于“C#在Excel表格中如何實現(xiàn)插入、編輯和刪除批注”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
當前文章:C#在Excel表格中如何實現(xiàn)插入、編輯和刪除批注-創(chuàng)新互聯(lián)
文章來源:http://www.jbt999.com/article48/ccphep.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站排名、用戶體驗、App開發(fā)、Google、品牌網(wǎng)站設計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容