Logotipo de Npoi

Npoi

Comunidad
znlgis
npoi

Use when reading/writing Excel (.xls/.xlsx) or Word (.doc/.docx) in .NET without Microsoft Office — XSSFWorkbook/HSSFWorkbook/SXSSFWorkbook, WorkbookFactory.Create, ICellStyle reuse, IFormulaEvaluator. Also for dates rendering as numbers, OutOfMemoryException on large files, and 64000 cell-style limits. NPOI: .NET port of Apache POI.

Descripción general

Editorznlgis
Repositorioopengis-skills
Nombre de la habilidadnpoi
Estrellas
76
Forks
12
Archivos incluidos
Solo instrucciones
LicenciaMIT
Enlaces
  • Instrucciones en Markdown

    Un archivo SKILL.md que el modelo carga cuando lo necesita, de modo que solo consume tokens cuando la petición encaja con la habilidad.

  • Funciona con cualquier LLM

    Las habilidades de IA están en Markdown, no en código específico de un proveedor, por lo que funcionan con GPT, Claude, Gemini, Grok o un modelo local.

  • Todo en las instrucciones

    Todo lo que necesita el modelo está en las instrucciones: no hay archivos adicionales que sincronizar.

  • Código abierto

    Publicada por znlgis en GitHub. Revisa el contenido original antes de instalarla.

Instalación

Instala la habilidad de IA Npoi en TypingMind para usarla con cualquier LLM, o añádela a otro agente que lea SKILL.md.

1

Instalar en TypingMind

TypingMind instala una habilidad directamente desde su carpeta de GitHub: lee SKILL.md, empaqueta los archivos de recursos y guarda el resultado localmente.

  1. Abre la aplicación y ve a Plugins → Habilidades.
  2. Elige «Instalar desde GitHub».
  3. Pega la URL de la carpeta de la habilidad que se muestra a continuación y confirma.
    https://github.com/znlgis/opengis-skills/tree/main/csharp/npoi
  4. Activa la habilidad en cada chat en el que quieras tenerla disponible.
Plugins → Habilidades → Añadir habilidad → URL de GitHub; después, pega la URL de la carpeta y pulsa Continuar.
2

Instalar en otro agente

Cualquier agente que lea el formato Agent Skills puede usar esta habilidad: copia la carpeta en el directorio de habilidades de ese agente.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/znlgis/opengis-skills.git /tmp/opengis-skills
mkdir -p .claude/skills
cp -r /tmp/opengis-skills/csharp/npoi .claude/skills/npoi
Reinicia Claude Code después de copiarla para que detecte la nueva habilidad.

Úsala en TypingMind

Activa Npoi en cualquier chat de TypingMind y el modelo se encargará del resto. Su nombre y descripción se incluyen en el prompt del sistema y, en cuanto una petición encaja con la habilidad, el modelo carga las instrucciones completas por sí solo. No tienes que invocarla manualmente y no consume tokens hasta que se utiliza.

El modelo carga Npoi automáticamente en cuanto una petición es relevante para esa habilidad.

Funciona con cualquier modelo de IA

Las habilidades de IA son instrucciones en Markdown, no código específico de un proveedor, por lo que Npoi no está vinculada al modelo para el que se creó. Instálala una sola vez en TypingMind y úsala con GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama o un modelo local que ejecutes tú, todo con tus propias claves API.

  • Se carga solo cuando se necesita

    El prompt del sistema solo incluye el nombre y la descripción. Las instrucciones se cargan con la primera petición que encaja con la habilidad, por lo que una habilidad que no se usa no supone ningún coste.

  • Cambia de modelo durante el chat

    Como la habilidad contiene instrucciones y no código, cambiar de modelo no impide que funcione: el nuevo modelo lee el mismo archivo SKILL.md.

Instrucciones de la habilidad

Este es el contenido de SKILL.md que carga el modelo. Léelo antes de instalar la habilidad: contiene instrucciones que tu modelo seguirá.

项目地址: https://github.com/nissl-lab/npoi

NuGet: NPOI

最新版本: 2.8.1(2026-10-07 仓库最后提交;截至 2026-10,NuGet 实测)

官方文档: https://github.com/nissl-lab/npoi/wiki

许可证: Apache-2.0

注意(OSMF): 自 2.8.0 起 NuGet 包附带 OSMF EULA,年收入超过 1 万美元的组织/商业用户需通过 GitHub Sponsors 支付月度维护费;初创、自由职业者与业余项目免费。详见 https://github.com/nissl-lab/npoi 与 https://opensourcemaintenancefee.org/consumers/。

概述

NPOI 主要能力:

  • xls(HSSF) / xlsx(XSSF) / 流式 xlsx(SXSSF) 读写
  • 公式计算:FormulaEvaluator
  • 样式:字体、边框、对齐、数据格式、条件格式
  • 图表 / 图片 / 批注
  • Word .doc(HWPF) / Word .docx(XWPF)
  • PowerPoint .ppt/.pptx
  • 跨平台:.NET Framework / .NET 6+ / Linux / macOS

大文件写入推荐 SXSSF(流式),可处理百万行 Excel;大文件读取推荐 EventModel(XSSF SAX)。


安装

方式一:dotnet CLI

bash
dotnet add package NPOI

方式二:PackageReference

xml
<ItemGroup>
  <PackageReference Include="NPOI" Version="2.8.1" />
</ItemGroup>

方式三:包管理器控制台

powershell
Install-Package NPOI -Version 2.8.1

验证安装

bash
dotnet list package | grep -i npoi

主要命名空间:

  • NPOI.HSSF.UserModel — xls
  • NPOI.XSSF.UserModel — xlsx
  • NPOI.XSSF.Streaming — 流式 xlsx
  • NPOI.SS.UserModel — 通用接口(IWorkbook / ISheet / IRow / ICell)
  • NPOI.SS.Util — CellRangeAddress、WorkbookUtil

核心 API 速查

对象入口说明
工作簿new XSSFWorkbook() / new HSSFWorkbook() / new SXSSFWorkbook(rowAccessWindow)xlsx / xls / 流式写大文件
读任意格式WorkbookFactory.Create(stream)按魔数自动识别 xls/xlsx
表wb.CreateSheet(name) / wb.GetSheetAt(i) / wb.GetSheet(name)
行 / 单元格sh.CreateRow(i) / row.CreateCell(j)
单元格值cell.SetCellValue(...) / cell.CellType枚举 CellType.String/Numeric/Boolean/Formula/Blank
样式wb.CreateCellStyle() + wb.CreateFont() + wb.CreateDataFormat()
公式求值wb.GetCreationHelper().CreateFormulaEvaluator()读公式结果用它,不要自己解析
WordXWPFDocument / XWPFParagraph.docx 读写

写入 Excel(xlsx)

csharp
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

IWorkbook wb = new XSSFWorkbook();
ISheet sh = wb.CreateSheet("Sheet1");

// 标题样式
ICellStyle headStyle = wb.CreateCellStyle();
IFont font = wb.CreateFont();
font.Boldweight = (short)FontBoldWeight.Bold; font.FontHeightInPoints = 12;
headStyle.SetFont(font);
headStyle.FillForegroundColor = IndexedColors.Grey25Percent.Index;
headStyle.FillPattern = FillPattern.SolidForeground;
headStyle.Alignment = HorizontalAlignment.Center;

string[] head = { "ID", "Name", "Score", "Time" };
IRow row = sh.CreateRow(0);
for (int i = 0; i < head.Length; i++) {
    var c = row.CreateCell(i);
    c.SetCellValue(head[i]);
    c.CellStyle = headStyle;
}

// 数据
for (int r = 1; r <= 10; r++) {
    var dr = sh.CreateRow(r);
    dr.CreateCell(0).SetCellValue(r);
    dr.CreateCell(1).SetCellValue("User" + r);
    dr.CreateCell(2).SetCellValue(80 + r);
    dr.CreateCell(3).SetCellValue(DateTime.Now);   // 注意要设置日期样式
}

// 列宽:AutoSizeColumn 依赖字体度量,Linux/容器里常失败,生产代码建议显式设宽
// (1/256 个字符宽,20 字符宽 ≈ 20 * 256)
for (int i = 0; i < head.Length; i++) sh.SetColumnWidth(i, 20 * 256);

using (var fs = new FileStream("out.xlsx", FileMode.Create))
{
    wb.Write(fs, leaveOpen: false);
}

读取 Excel

csharp
using var fs = new FileStream("in.xlsx", FileMode.Open, FileAccess.Read);
IWorkbook wb = WorkbookFactory.Create(fs);   // 自动识别 xls/xlsx
ISheet sh = wb.GetSheetAt(0);

for (int r = sh.FirstRowNum; r <= sh.LastRowNum; r++) {
    IRow row = sh.GetRow(r); if (row == null) continue;
    for (int c = row.FirstCellNum; c < row.LastCellNum; c++) {
        ICell cell = row.GetCell(c);
        Console.Write(GetValue(cell) + "\t");
    }
    Console.WriteLine();
}

static object GetValue(ICell? cell) => cell?.CellType switch {
    CellType.Numeric => DateUtil.IsCellDateFormatted(cell) ? cell.DateCellValue : cell.NumericCellValue,
    CellType.String  => cell.StringCellValue,
    CellType.Boolean => cell.BooleanCellValue,
    CellType.Formula => cell.CachedFormulaResultType switch {
        CellType.Numeric => cell.NumericCellValue,
        CellType.String  => cell.StringCellValue,
        _ => ""
    },
    _ => ""
};

数据格式(日期 / 数字)

csharp
ICellStyle dateStyle = wb.CreateCellStyle();
dateStyle.DataFormat = wb.CreateDataFormat().GetFormat("yyyy-MM-dd HH:mm:ss");

ICellStyle pctStyle = wb.CreateCellStyle();
pctStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");

公式

csharp
sh.CreateRow(11).CreateCell(2).SetCellFormula("SUM(C2:C11)");

// 计算公式(写出前)
var ev = wb.GetCreationHelper().CreateFormulaEvaluator();
ev.EvaluateAll();

流式写入(SXSSF,海量行)

csharp
using NPOI.XSSF.Streaming;

using SXSSFWorkbook wb = new SXSSFWorkbook(rowAccessWindowSize: 1000);
ISheet sh = wb.CreateSheet("big");
for (int r = 0; r < 1_000_000; r++) {
    var row = sh.CreateRow(r);
    for (int c = 0; c < 10; c++)
        row.CreateCell(c).SetCellValue(r * 10 + c);
}
using (var fs = File.Create("big.xlsx"))
{
    wb.Write(fs);
}
wb.Dispose();   // SXSSF 必须 Dispose 才会清理磁盘临时文件

合并单元格 / 冻结 / 筛选

csharp
sh.AddMergedRegion(new CellRangeAddress(0, 0, 0, 3));   // 合并第一行 A:D
sh.CreateFreezePane(0, 1);                              // 冻结首行
sh.SetAutoFilter(new CellRangeAddress(0, sh.LastRowNum, 0, 3));

图片

csharp
byte[] img = File.ReadAllBytes("logo.png");
int picIdx = wb.AddPicture(img, PictureType.PNG);
IDrawing patriarch = sh.CreateDrawingPatriarch();
var anchor = wb.GetCreationHelper().CreateClientAnchor();
anchor.Col1 = 5; anchor.Row1 = 1;
var pic = patriarch.CreatePicture(anchor, picIdx);
pic.Resize();

条件格式

csharp
var cf = (XSSFConditionalFormatting)sh.SheetConditionalFormatting;
var rule = cf.CreateConditionalFormattingRule(ComparisonOperator.GreaterThan, "60");
var pattern = rule.CreatePatternFormatting();
pattern.FillBackgroundColor = IndexedColors.Yellow.Index;
cf.AddConditionalFormatting(new[] { CellRangeAddress.ValueOf("C2:C11") }, rule);

Word(XWPF)

csharp
using NPOI.XWPF.UserModel;

XWPFDocument doc = new();
XWPFParagraph p  = doc.CreateParagraph();
XWPFRun r = p.CreateRun();
r.FontSize = 16; r.IsBold = true; r.SetText("Hello NPOI");

using var fs = File.Create("out.docx");
doc.Write(fs);

性能与最佳实践

  1. 百万级写用 SXSSF,注意 Dispose() 清理 tmp 文件
  2. 样式数量受 64000 限制:复用 ICellStyle 实例
  3. 日期单元格 必须设置 DataFormat,否则显示数字
  4. 大文件读用 XSSFEventModel(SAX)以减少内存
  5. xls 行数上限 65536,xlsx 上限 1048576
  6. 避免在循环内 wb.CreateCellStyle()

典型工作流

工作流一:导出报表(写 xlsx)

  1. new XSSFWorkbook() → CreateSheet(),先铺数据:CreateRow() / CreateCell() / SetCellValue()
  2. 再设样式:循环外建好 ICellStyle / IFont / IDataFormat 实例复用,日期列套带日期格式的样式
  3. 有公式要提前 CreateFormulaEvaluator().EvaluateAll() 把结果算出来
  4. FileStream 写出后关闭;服务端环境无需装 Office(纯托管)

工作流二:导入解析(读 xls/xlsx)

  1. FileStream → WorkbookFactory.Create(fs) 自动识别格式
  2. GetSheetAt(0)(或按名字取表)→ 遍历 Row/Cell
  3. 按 CellType 分支取值:String/Numeric/Boolean/Formula(公式用 IFormulaEvaluator 求值)
  4. 数据入库前做格式校验,行数接近上限时(xls 65536 / xlsx 1048576)考虑拆分

工作流三:百万行导出

  1. 写用 new SXSSFWorkbook(rowAccessWindowSize: 1000) 流式滚动,只保留窗口内的行在内存
  2. 用完后 Dispose() 清理临时文件
  3. 读大文件改用 XSSFEventModel(SAX)遍历

常见问题

问题解决
中文乱码文本本身是 UTF-8,.xlsx 不会乱码;.xls(HSSF)在 Excel 里按本地代码页显示,建议中文场景统一用 xlsx
日期变数字(显示 45000 之类)单元格要套带日期 DataFormat 的 ICellStyle,只 SetCellValue(DateTime) 不够
Office 报「此工作簿包含无法读取的内容」多为样式数超 64000 或公式非法:把 CreateCellStyle() 提到循环外复用,并检查 SetCellFormula 的表达式
大文件 OutOfMemoryException写用 SXSSFWorkbook(记得 Dispose() 清临时文件),读用 XSSF EventModel(SAX)
打开提示文件损坏wb.Write(fs) 要写在 FileStream 作用域内,且不要把 leaveOpen 误设为 true 后又提前 dispose 流
AutoSizeColumn 抛异常或列宽为 0xlsx 下自动列宽依赖字体度量,Linux/容器里常失败;改成显式 SetColumnWidth(col, width*256)

AI 使用建议

推荐工作流

  1. 确定格式:.xls 用 HSSFWorkbook,.xlsx 用 XSSFWorkbook,不确定用 WorkbookFactory.Create() 自动识别
  2. 读文件:FileStream → WorkbookFactory.Create() → GetSheetAt(0) → 遍历 Row/Cell → 根据 CellType 取值
  3. 写文件:new XSSFWorkbook() → CreateSheet() → CreateRow() → CreateCell() → SetCellValue() → FileStream 写出
  4. 样式后设:先写数据,再设样式(复用 ICellStyle 实例,避免 64000 限制)
  5. 大文件处理:写用 SXSSFWorkbook(流式),读用 EventModel(SAX 事件驱动)

关键模式与常见陷阱

  • 日期变数字:日期单元格必须设置带日期 DataFormat 的 ICellStyle,否则显示为数字
  • 样式 64000 限制:ICellStyle 实例总数不能超过 64000,必须在循环外 CreateCellStyle() 并复用
  • 中文不乱码的前提:xlsx 默认 UTF-8 不会乱码;xls 需要检查 Excel 打开时自动识别
  • 公式需要求值:wb.GetCreationHelper().CreateFormulaEvaluator().EvaluateAll() 在写出前调用
  • SXSSF 记得 Dispose:流式写入后必须 Dispose() 清理临时文件
  • 不要用 COM/Interop:NPOI 纯托管,不依赖 Office 安装,服务器环境友好

如何选择正确方案

场景推荐方案
简单导入导出(< 10 万行)NPOI XSSFWorkbook
百万级写入NPOI SXSSFWorkbook
百万级读取NPOI EventModel(SAX)
WinForms/WPF 内嵌表格ReoGrid(底层用 NPOI 读写文件)
纯数据交换(无格式)MiniExcel / CsvHelper

相关技能

  • reogrid — .NET 电子表格控件,内嵌可编辑表格,底层依赖 NPOI 读写 Excel 文件:../reogrid/SKILL.md
  • sqlsugar — .NET ORM,可与 NPOI 配合:从数据库查询后批量导出 Excel:../sqlsugar/SKILL.md
  • sod — 另一套 .NET ORM,导出场景同样先查库再交给 NPOI 写表:../sod/SKILL.md

参考资源

Preguntas frecuentes

¿Qué hace la habilidad de IA Npoi?

Use when reading/writing Excel (.xls/.xlsx) or Word (.doc/.docx) in .NET without Microsoft Office — XSSFWorkbook/HSSFWorkbook/SXSSFWorkbook, WorkbookFactory.Create, ICellStyle reuse, IFormulaEvaluator. Also for dates rendering as numbers, OutOfMemoryException on large files, and 64000 cell-style limits. NPOI: .NET port of Apache POI.

¿Por qué usar Npoi en TypingMind?

Porque la instalas una vez y la usas con cualquier modelo. Npoi está escrita en Markdown, no en código específico de un proveedor, por lo que la misma habilidad funciona con GPT-5, Claude, Gemini, Grok o un modelo local, y puedes cambiar de modelo en mitad del chat sin que deje de funcionar. TypingMind utiliza tus propias claves API, de modo que pagas directamente a los proveedores en lugar de pagar una suscripción por puesto, y tus habilidades y chats permanecen en tu propio almacenamiento.

¿Cómo instalo Npoi en TypingMind?

Abre Plugins → Habilidades → Instalar desde GitHub en TypingMind y pega https://github.com/znlgis/opengis-skills/tree/main/csharp/npoi. TypingMind lee el archivo SKILL.md de esa carpeta e instala su contenido como una habilidad que puedes activar en cada chat.

¿Qué modelos de IA pueden usar Npoi?

Cualquier modelo que conectes en TypingMind. Las habilidades de IA son instrucciones en Markdown, no código específico de un proveedor, por lo que GPT, Claude, Gemini, Grok y los modelos locales pueden cargar esta habilidad cuando una solicitud se corresponda con ella.

¿Con cuántos modelos de IA puedo usar Npoi?

Con tantos como quieras. Siempre que un modelo admita habilidades, puedes usar Npoi con él: GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama y muchos más, todos en TypingMind con tus propias claves API.

¿Es gratuita la habilidad de IA Npoi?

Sí. znlgis la ha publicado en GitHub bajo la licencia MIT. Solo pagas a tu proveedor de IA por los tokens que utilizas.

¿Qué son las habilidades de IA?

Una habilidad de IA es un conjunto de instrucciones reutilizables que enseña a un modelo de IA a realizar una tarea concreta. Sigue el formato abierto Agent Skills: un archivo SKILL.md con un nombre y una descripción, además de los scripts, plantillas o archivos de referencia que pueda necesitar el modelo. El modelo solo lee las instrucciones cuando tu solicitud se corresponde con la habilidad, por lo que una habilidad instalada no supone ningún coste hasta que se utiliza.

¿En qué se diferencian las habilidades de IA de los plugins o los servidores MCP?

Un plugin o un servidor MCP proporciona al modelo nuevas herramientas que puede invocar: código que se ejecuta en algún lugar y devuelve un resultado. En cambio, una habilidad de IA aporta conocimientos y procedimientos al modelo: cómo abordar una tarea, qué pasos seguir y qué se considera un buen resultado. Las habilidades son instrucciones en Markdown, por lo que no necesitan servidor, clave API ni entorno de ejecución, y funcionan con cualquier modelo.

Ver el directorio

Crea ya tu propio espacio de trabajo con IA

Suscríbete a nuestro boletín para recibir avisos sobre nuevas funciones y próximos sorteos 👇