NPOI 是 POI 项目的 .NET 版本。POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目, 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写。NPOI是构建在POI 3.x版本之上的,它可以在没有安装Office的情况下对Word/Excel文档进行读写操作。本文主要介绍.NET Core(C#)中,使用NPOI将Excel(.xls,.xlsx)文件添加批注的方法,以及相关的示例代码。

1、安装引用DotNetCore.NPOI

1)使用Nuget界面管理器

搜索"DotNetCore.NPOI",在列表中找到它,点击"安装"

相关文档VS(Visual Studio)中Nuget的使用

2)使用Package Manager命令安装

PM> Install-Package DotNetCore.NPOI

3)使用.NET CLI命令安装

> dotnet add TodoApi.csproj package DotNetCore.NPOI

2、使用NPOI添加批注的示例代码

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("Sheet1");
HSSFRow row = (HSSFRow)sheet.CreateRow(0);
HSSFCell cell = (HSSFCell)row.CreateCell(0);
cell.SetCellValue("Cell1");
// 创建绘图主控制器(用于包括单元格注释在内的所有形状的顶级容器)
IDrawing patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
// 客户端锚定定义工作表中注释的大小和位置
//(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2) 
//前四个参数是坐标点,后四个参数是编辑和显示批注时的大小.
IComment comment = patriarch.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 2, 1, 4, 4));
// 设置批注作者
comment.Author = "Author";
// 设置批注内容
comment.String = new HSSFRichTextString($"{comment.Author}:{Environment.NewLine}A comment");
// 让作者像Excel一样在顶部以粗体显示
// 当鼠标停留在已注释的单元格上时,作者将显示在状态栏中
IFont font = workbook.CreateFont();
font.Boldweight = (short)FontBoldWeight.Bold;
comment.String.ApplyFont(0, comment.Author.Length, font);
// 设置批注显示
comment.Visible = true;
// 为单元格分配注释
cell.CellComment = comment;
using (MemoryStream exportData = new MemoryStream())
{
    workbook.Write(exportData);
}

3、完整示例代码

using System;
using System.Text;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;


namespace SetCellCommentInXls
{
    class Program
    {
        static HSSFWorkbook hssfworkbook;
        static void Main(string[] args)
        {
            InitializeWorkbook();
            ISheet sheet = hssfworkbook.CreateSheet("ICell comments in POI HSSF");
            //创建绘图族长。这是所有形状(包括单元格注释)的顶级容器。
            IDrawing patr = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
            //在第3行创建一个单元格
            ICell cell1 = sheet.CreateRow(3).CreateCell(1);
            cell1.SetCellValue(new HSSFRichTextString("Hello, World"));
            //anchor定义工作表中注释的大小和位置
            IComment comment1 = patr.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 4, 2, 6, 5));
            // 在注释中设置文本
            comment1.String = (new HSSFRichTextString("We can set comments in POI"));
            //设置评论作者。
            //当您将鼠标移到已注释的单元格上时,您可以在状态栏中看到它
            comment1.Author = ("Apache Software Foundation");
            // 向单元格分配注释的第一种方法是通过HSSFCell。SetCellComment方法
            cell1.CellComment = (comment1);
            //在第6行中创建另一个单元格
            ICell cell2 = sheet.CreateRow(6).CreateCell(1);
            cell2.SetCellValue(36.6);

            HSSFComment comment2 = (HSSFComment)patr.CreateCellComment(new HSSFClientAnchor(0, 0, 0, 0, 4, 8, 6, 11));
            //修改评论的背景颜色
            comment2.SetFillColor(204, 236, 255);
            HSSFRichTextString str = new HSSFRichTextString("Normal body temperature");
            //对注释中的文本应用自定义字体
            IFont font = hssfworkbook.CreateFont();
            font.FontName = ("Arial");
            font.FontHeightInPoints = 10;
            font.IsBold = true;
            font.Color = HSSFColor.Red.Index;
            str.ApplyFont(font);
            comment2.String = str;
            comment2.Visible = true; //默认情况下注释是隐藏的。这个总是可见的。
            comment2.Author = "Bill Gates";
            / * *
            *为单元格分配注释的第二种方法是隐式指定其行和列。
            *注意,可以设置一个不存在的单元格的行和列。
            它是有效的,commnet是可见的。
            * /
            comment2.Row = 6;
            comment2.Column = 1;
            WriteToFile();
        }
        static void WriteToFile()
        {
            //将工作簿的流数据写入根目录
            FileStream file = new FileStream(@"test.xls", FileMode.Create);
            hssfworkbook.Write(file);
            file.Close();
        }
        static void InitializeWorkbook()
        {
            hssfworkbook = new HSSFWorkbook();
            //创建一个DocumentSummaryInformation条目
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "NPOI Team";
            hssfworkbook.DocumentSummaryInformation = dsi;
            //创建一个汇总信息条目
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = "NPOI SDK Example";
            hssfworkbook.SummaryInformation = si;
        }
    }
}

NOPI源码https://github.com/dotnetcore/NPOI

推荐文档