標籤:啟動 providing span print names 處理 使用 [] rod
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批註“哪種開發語言最好”