標籤:儲存格 excel net 注釋
Excel批註常用於為個別的儲存格添加註釋,讀者能夠從注釋中擷取額外的資訊。批註可隱藏,只會在儲存格右上方顯示紅色三角,添加後不會對儲存格的內容喧賓奪主。在日常編程處理Excel中,為個別儲存格添加備忘資訊,也有必要。這篇博文主要介紹使用免費版的Spire.XLS在C#中為儲存格添加備忘,並設定位置,大小,富文本及文本對齊。
想嘗試的朋友可以從以下三個地址下載Free Spire.XLS: E-iceblue官網下載;Nuget;CSDN下載。下載之後,請將bin 檔案夾裡的.dll添加為VS的引用。
步驟一: 建立一個新的工作薄和表單。
Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0];
步驟二:啟用類ExcelFont添加設定字型,在設定comment文本時會用到。
ExcelFont font1 = workbook.CreateFont(); font1.FontName = "仿宋"; font1.Color = Color.Red; font1.IsBold = true; font1.Size = 12; ExcelFont font2 = workbook.CreateFont(); font2.FontName = "仿宋"; font2.Color = Color.Blue; font2.Size = 12; font2.IsBold = true; ExcelFont font3 = workbook.CreateFont(); font3.FontName = "Calibri"; font3.Color = Color.Blue; font3.Size = 12; font3.IsBold = true;
步驟三:為儲存格F5添加批註1,設定其大小,位置,文本,文本對齊。
ExcelComment Comment1 = sheet.Range["F5"].Comment; Comment1.IsVisible = true; //設定批註高度和寬度 Comment1.Height = 150; Comment1.Width = 300; //設定批註位置 Comment1.Top = 20; Comment1.Left = 40; //設定常值內容,對齊,文本旋轉 Comment1.RichText.Text = "為了防止人類齊心協力開發出人工智慧,上帝給了程式員不同的開發語言。但哪種語言才是最好的呢?"; Comment1.RichText.SetFont(0, 32, font2); Comment1.RichText.SetFont(33, 44, font1); Comment1.TextRotation = TextRotationType.LeftToRight; Comment1.VAlignment = CommentVAlignType.Center; Comment1.HAlignment = CommentHAlignType.Justified;
步驟四:添加批註2作為對照。
ExcelComment Comment2= sheet.Range["F14"].Comment; Comment2.IsVisible = true; Comment2.Height = 150; Comment2.Width = 300; Comment2.RichText.Text = "About E-iceblue: \nE-iceblue focuses on providing excellent office components for developers to operate Word, Excel, PDF, and PowerPoint documents."; Comment2.TextRotation = TextRotationType.LeftToRight; Comment2.RichText.SetFont(0, 16, font2); Comment2.Top = 170; Comment2.Left = 450; Comment2.VAlignment = CommentVAlignType.Top; Comment2.HAlignment = CommentHAlignType.Justified;
步驟五:儲存文檔,並啟動查看效果。
workbook.SaveToFile("S3.xlsx", ExcelVersion.Version2010); System.Diagnostics.Process.Start("S3.xlsx");
:
全部代碼:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Spire.Xls;using System.Drawing;namespace How_to_set_Excel_margin_to_print{ class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0]; ExcelFont font1 = workbook.CreateFont(); font1.FontName = "仿宋"; font1.Color = Color.Red; font1.IsBold = true; font1.Size = 12; ExcelFont font2 = workbook.CreateFont(); font2.FontName = "仿宋"; font2.Color = Color.Blue; font2.Size = 12; font2.IsBold = true; ExcelFont font3 = workbook.CreateFont(); font3.FontName = "Calibri"; font3.Color = Color.Blue; font3.Size = 12; font3.IsBold = true; ExcelComment Comment1 = sheet.Range["F5"].Comment; Comment1.IsVisible = true; Comment1.Height = 150; Comment1.Width = 300; Comment1.Top = 20; Comment1.Left = 40; Comment1.RichText.Text = "為了防止人類齊心協力開發出人工智慧,上帝給了程式員不同的開發語言。但哪種語言才是最好的呢?"; Comment1.RichText.SetFont(0, 32, font2); Comment1.RichText.SetFont(33, 44, font1); Comment1.TextRotation = TextRotationType.LeftToRight; Comment1.VAlignment = CommentVAlignType.Center; Comment1.HAlignment = CommentHAlignType.Justified; ExcelComment Comment2= sheet.Range["F14"].Comment; Comment2.IsVisible = true; Comment2.Height = 150; Comment2.Width = 300; Comment2.RichText.Text = "About E-iceblue: \nE-iceblue focuses on providing excellent office components for developers to operate Word, Excel, PDF, and PowerPoint documents."; Comment2.TextRotation = TextRotationType.LeftToRight; Comment2.RichText.SetFont(0, 16, font2); Comment2.Top = 170; Comment2.Left = 450; Comment2.VAlignment = CommentVAlignType.Top; Comment2.HAlignment = CommentHAlignType.Justified; workbook.SaveToFile("S3.xlsx", ExcelVersion.Version2010); System.Diagnostics.Process.Start("S3.xlsx"); } }}
為方便大家參考閱讀使用,博主將陸續漢化E-iceblue控制項的使用教程。感興趣的朋友請關注收藏此部落格。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
C# Excel批註“哪種開發語言最好”