WeihanLi.Npoi 1.16.0 Release Notes
WeihanLi.Npoi 1.16.0 Release Notes
Intro
最近有网友咨询如何设置单元格样式,在之前的版本中是不支持的,之前主要考虑的是数据,对于导出的样式并没有支持,这个 issue 也让我觉得目前还不是足够的灵活,于是进行了一些探索,增加了更多扩展的可能性,一起来看一下吧
Sheet Setting
因为导入导出是针对某一个 Sheet 而言的,所以支持为不同的 Sheet 设置不同的配置,如果所有 Sheet 的配置都是一样的,则只配置默认的 Sheet 配置就可以了,新版本中增加了三个委托,进一步的增强的导出的灵活性
/// <summary>
/// Cell Action on export
/// </summary>
public Action<ICell>? CellAction { get; set; }
/// <summary>
/// Row Action on export
/// </summary>
public Action<IRow>? RowAction { get; set; }
/// <summary>
/// Sheet Action on export
/// </summary>
public Action<ISheet>? SheetAction { get; set; }
可以不同的 Level 扩展自己想要的功能,这些扩展会在写入数据之后执行,可以借此来设置特殊单元格的样式,如果你想也可以重新设置导出的数据,来看下面的示例:
Sample1
首先来看一个设置 Header 字体样式的一个示例,完整代码参考:https://github.com/WeihanLi/WeihanLi.Npoi/blob/310d7637e82210f7558b2a60a637b43485c0e562/samples/DotNetCoreSample/Program.cs#L157
var setting = FluentSettings.For<TestEntity>();
// ExcelSetting
setting.HasAuthor("WeihanLi")
.HasTitle("WeihanLi.Npoi test")
.HasDescription("WeihanLi.Npoi test")
.HasSubject("WeihanLi.Npoi test");
setting.HasSheetSetting(config =>
{
config.StartRowIndex = 1;
config.SheetName = "SystemSettingsList";
config.AutoColumnWidthEnabled = true;
config.RowAction = row =>
{
if (row.RowNum == 0)
{
var style = row.Sheet.Workbook.CreateCellStyle();
style.Alignment = HorizontalAlignment.Center;
var font = row.Sheet.Workbook.CreateFont();
font.FontName = "JetBrains Mono";
font.IsBold = true;
font.FontHeight = 200;
style.SetFont(font);
row.Cells.ForEach(c => c.CellStyle = style);
}
};
});
setting.Property(_ => _.SettingId)
.HasColumnIndex(0);
setting.Property(_ => _.SettingName)
.HasColumnTitle("SettingName")
.HasColumnIndex(1);
setting.Property(_ => _.DisplayName)
.HasOutputFormatter((entity, displayName) => $"AAA_{entity?.SettingName}_{displayName}")
.HasInputFormatter((entity, originVal) => originVal?.Split(new[] { '_' })[2])
.HasColumnTitle("DisplayName")
.HasColumnIndex(2);
setting.Property(_ => _.SettingValue)
.HasColumnTitle("SettingValue")
.HasColumnIndex(3);
setting.Property(_ => _.CreatedTime)
.HasColumnTitle("CreatedTime")
.HasColumnIndex(4)
.HasColumnWidth(10)
.HasColumnFormatter("yyyy-MM-dd HH:mm:ss");
setting.Property(_ => _.CreatedBy)
.HasColumnInputFormatter(x => x += "_test")
.HasColumnIndex(4)
.HasColumnTitle("CreatedBy");
setting.Property(x => x.Enabled)
.HasColumnInputFormatter(val => "Enabled".EqualsIgnoreCase(val))
.HasColumnOutputFormatter(v => v ? "Enabled" : "Disabled");
setting.Property("HiddenProp")
.HasOutputFormatter((entity, val) => $"HiddenProp_{entity?.PKID}");
setting.Property(_ => _.PKID).Ignored();
setting.Property(_ => _.UpdatedBy).Ignored();
setting.Property(_ => _.UpdatedTime).Ignored();
}
导出代码:
var entities = new List<TestEntity>()
{
new TestEntity()
{
PKID = 1,
SettingId = Guid.NewGuid(),
SettingName = "Setting1",
SettingValue = "Value1",
DisplayName = "dd\"d,1"
},
new TestEntity()
{
PKID=2,
SettingId = Guid.NewGuid(),
SettingName = "Setting2",
SettingValue = "Value2",
Enabled = true,
CreatedBy = "li\"_"
},
};
entities.ToExcelFile("test.xlsx");
导出结果如下:

可以看得出来,我们导出的结果中第一行的样式和别的样式会不同
Another Sample
除了直接导出到 excel 文件之外,还可以支持导出到某一个 sheet 中,我在我的另外一个 DbTool 的项目中也在使用,将原来的一大坨代码做了很大的简化,详细修改可以参考这个 commit: https://github.com/WeihanLi/DbTool/commit/7c7b980714af11e5fec3f8b3babac0904f94cff3#diff-d0ef24afd15ae6aa4ead43fbe31a895c1c051ab9c6e50f4ff4c7544a9592f958R8
最后实际使用的导出代码:
var workbook = ExcelHelper.PrepareWorkbook(FileExtension.EndsWith(".xls") ? ExcelFormat.Xls : ExcelFormat.Xlsx);
foreach (var tableEntity in tableInfo)
{
//Create Sheet
var sheet = workbook.CreateSheet(tableEntity.TableName);
//create title
var titleRow = sheet.CreateRow(0);
var titleCell = titleRow.CreateCell(0);
titleCell.SetCellValue(tableEntity.TableDescription);
// export list data to excel
sheet.ImportData(tableEntity.Columns);
}
return workbook.ToExcelBytes();
相比之前的代码,大大简化了,原来的代码可以参考https://github.com/WeihanLi/DbTool/commit/7c7b980714af11e5fec3f8b3babac0904f94cff3#diff-d0ef24afd15ae6aa4ead43fbe31a895c1c051ab9c6e50f4ff4c7544a9592f958L17-L121, 100 多行代码,太长了不方便截图
大部分的代码都变成了配置,部分配置代码可以参考下面的代码,具体配置可以参考:https://github.com/WeihanLi/DbTool/blob/wpf-dev/src/DbTool/ExcelDbDocExporter.cs
var settings = FluentSettings.For<ColumnEntity>();
settings.HasExcelSetting(x =>
{
x.Author = "DbTool";
})
.HasSheetSetting(x =>
{
x.StartRowIndex = 2;
x.AutoColumnWidthEnabled = true;
x.RowAction = row =>
{
// apply header row style
if (row.RowNum == 1)
{
var headerStyle = row.Sheet.Workbook.CreateCellStyle();
headerStyle.Alignment = HorizontalAlignment.Center;
var headerFont = row.Sheet.Workbook.CreateFont();
headerFont.FontHeight = 180;
headerFont.IsBold = true;
headerFont.FontName = "微软雅黑";
headerStyle.SetFont(headerFont);
row.Cells.ForEach(c => c.CellStyle = headerStyle);
}
};
x.SheetAction = sheet =>
{
// set merged region
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 6));
// apply title style
var titleStyle = sheet.Workbook.CreateCellStyle();
titleStyle.Alignment = HorizontalAlignment.Left;
var font = sheet.Workbook.CreateFont();
font.FontHeight = 200;
font.FontName = "微软雅黑";
font.IsBold = true;
titleStyle.SetFont(font);
titleStyle.FillBackgroundColor = IndexedColors.Black.Index;
titleStyle.FillForegroundColor = IndexedColors.SeaGreen.Index;
titleStyle.FillPattern = FillPattern.SolidForeground;
sheet.GetRow(0).GetCell(0).CellStyle = titleStyle;
};
});
// ...
上面的代码通过 RowAction 配置了 Header 行的单元格的样式,通过 SheetAction 配置了 Title 行的单元格合并和 Title 单元格的样式,导出结果如下图所示:

More
WeihanLi.Npoi 通过 Fluent API 的方式提供了非常灵活的导入导出配置,如果你也有导入导出 Excel 的需求,可以了解一下,如果不能满足的地方或者使用过程中有遇到任何问题欢迎给我提 issue https://github.com/WeihanLi/WeihanLi.Npoi/issues/new/choose
其他介绍文章:
更多的使用文档可以参考项目示例项目,单元测试以及文档
- 示例项目:https://github.com/WeihanLi/WeihanLi.Npoi/blob/dev/samples/DotNetCoreSample/Program.cs
- 单元测试:https://github.com/WeihanLi/WeihanLi.Npoi/blob/dev/test/WeihanLi.Npoi.Test/ExcelTest.cs
- 文档:https://weihanli.github.io/WeihanLi.Npoi/index.html
References
- https://github.com/WeihanLi/WeihanLi.Npoi/issues/104
- https://github.com/WeihanLi/DbTool/blob/wpf-dev/src/DbTool/ExcelDbDocExporter.cs
- https://weihanli.github.io/WeihanLi.Npoi/index.html
- https://www.nuget.org/packages/WeihanLi.Npoi/1.16.0
WeihanLi.Npoi 1.16.0 Release Notes的更多相关文章
- WeihanLi.Npoi 1.14.0 Release Notes
WeihanLi.Npoi 1.14.0 Release Notes Intro 周末更新了一下项目,开始使用可空引用类型,并且移除了 net45 的支持,仅支持 netstandard2.0 Cha ...
- WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes Intro 最近 NPOI 扩展新更新了两个版本,感谢 shaka chow 的帮忙和支持,这两个 Feature ...
- Git for Windows v2.11.0 Release Notes
homepage faq contribute bugs questions Git for Windows v2.11.0 Release Notes Latest update: December ...
- WeihanLi.Npoi 1.13.0 更新日志
WeihanLi.Npoi 1.13.0 更新日志 Intro 在 Github 上收到 Issue 收到网友反馈希望支持自动分 Sheet 导出,有兴趣的可以参考 Issue https://git ...
- ASP.NET Core 1.1.0 Release Notes
ASP.NET Core 1.1.0 Release Notes We are pleased to announce the release of ASP.NET Core 1.1.0! Antif ...
- Yasm 1.3.0 Release Notes
Yasm 1.3.0 Release Notes http://yasm.tortall.net/releases/Release1.3.0.html Target Audience Welcome ...
- WeihanLi.Npoi 1.7.0 更新
WeihanLi.Npoi 1.7.0 更新介绍 Intro 昨天晚上发布了 WeihanLi.Npoi 1.7.0 版本,增加了 ColumnInputFormatter/ColumnOutputF ...
- WeihanLi.Npoi 1.10.0 更新日志
WeihanLi.Npoi 1.10.0 更新日志 Intro 上周有个网友希望能够导入Excel时提供一个 EndRowIndex 来自己控制结束行和根据字段过滤的,周末找时间做了一下这个 feat ...
- MongoDB 3.0 Release Notes
MongoDB 3.0支持WiredTiger存储引擎,提供可插拔存储引擎API,新增SCRAM-SHA-1认证机制,改进explain功能. 可插拔存储引擎API 允许第三方为MongoDB开发存储 ...
随机推荐
- CodeForces - 1209D 并查集
题意: 有 n个不同的糖果,从 1到 n编号.有 k个客人.要用糖果招待客人.对于每个客人,这些糖果中恰有两个是其最爱.第 i个客人最爱的糖果编号是 xi和 y.将 k 个客人任意排列,他们按顺序去拿 ...
- Link/Cut Tree CodeForces - 614A 暴力+爆 long long 处理
题意: 给你一个区间[l,r],让你从小到大输出k^x,设y=k^x,要保证y在区间[l,r]中 题解: 就算k是最小的2也不需要枚举多少次就到long long的极限了,所以暴力没商量,根本不会TL ...
- keras fit_generator 并行
虽然已经走在 torch boy 的路上了, 还是把碰到的这个坑给记录一下 数据量较小时,我们可直接把整个数据集 load 到内存里,用 model.fit() 来拟合模型. 当数据集过大比如几十个 ...
- [Python] Pandas 中 Series 和 DataFrame 的用法笔记
目录 1. Series对象 自定义元素的行标签 使用Series对象定义基于字典创建数据结构 2. DataFrame对象 自定义行标签和列标签 使用DataFrame对象可以基于字典创建数据结构 ...
- Zabbix 监控项更多用法
监控服务端口状态 配置 Zabbix 提供的检测器 配置自定义值映射 查看监控项数据状态 触发器配置 自定义监控项 TCP 11 种状态 TCP 11 种状态 LISTEN - 侦听来自远方TCP端口 ...
- 僵尸进程 & 孤儿进程
参考博文 基本概念 僵尸进程:是所有进程都会进入的一种进程状态,子进程退出,而父进程并没有调用 wait() 或 waitpid() 获取子进程的状态信息,那么子进程的 PID 和 进程描述符 等资源 ...
- Nginx 动静分离概述
目录 Nginx 动静分离是什么 Nginx 动静分离配置 Nginx 动静分离是什么 Nginx 动静分离,指的是静态资源请求由 Nginx 处理,动态资源请求由 php-fpm 处理或 tomca ...
- codeforces 1042D - Petya and Array【树状数组+离散化】
题目:戳这里 题意:有n个数,问有多少个区间满足[L,R]内的和小于t. 解题思路: [L,R]内的和小于t等价于sum[R]-sum[L-1]<t,将sum[L-1]左移,可以看出R与L的关系 ...
- Python源码剖析——02虚拟机
<Python源码剖析>笔记 第七章:编译结果 1.大概过程 运行一个Python程序会经历以下几个步骤: 由解释器对源文件(.py)进行编译,得到字节码(.pyc文件) 然后由虚拟机按照 ...
- Netty(四)基于Netty 的简易版RPC
3.1 RPC 概述 下面的这张图,大概很多小伙伴都见到过,这是 Dubbo 官网中的一张图描述了项目架构的演进过程 它描述了每一种架构需要的具体配置和组织形态.当网站流量很小时,只需一个应用,将所有 ...