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

Excel批注常用于为个别的单元格添加注释,读者能够从注释中获取额外的信息。批注可隐藏,只会在单元格右上方显示红色三角,添加后不会对单元格的内容喧宾夺主。在日常编程处理Excel中,为个别单元格添加备注信息,也有必要。这篇博文主要介绍使用免费版的Spire.XLS在C#中为单元格添加备注,并设置位置,大小,富文本及文本对齐方式。

想尝试的朋友可以从以下三个地址下载Free Spire.XLS: E-iceblue官网下载NugetCSDN下载。下载之后,请将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控件的使用教程。感兴趣的朋友请关注收藏此博客。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值