首页 > 代码库 > C# Excel批注“哪种开发语言最好”

C# Excel批注“哪种开发语言最好”

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控件的使用教程。

感兴趣的朋友请关注收藏此博客。

<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

C# Excel批注“哪种开发语言最好”