WeihanLi.Npoi 根據模板匯出Excel
Intro
原來的匯出方式比較適用于比較簡單的匯出,每一條資料在一行,資料列雖然自定義程度比較高,如果要一條資料對應多行就做不到了,于是就想支持根據模板匯出,在 1.8.0 版本中引入了根據模板匯出的功能
使用示例
示例模板

模板規劃的可以有三種資料:
- Global:一個是匯出的時候可以指定一些引數,作為 Global 引數,默認引數格式使用:
$(Global:PropName)的格式 - Header:配置的對應屬性的顯示名稱,默認是屬性名稱,默認引數格式:
$(Header:PropName) - Data:對應資料的屬性值,默認引數格式:
$(Data:PropName)
默認模板引數格式(從 1.8.2 版本開始支持通過 TemplateHelper.ConfigureTemplateOptions 方法來自定義):
- Global 引數:
$(Global:{0}) - Header 引數:
$(Header:{0}) - Data 引數:
$(Data:{0}) - Data Begin:
<Data> - Data End:
</Data>
模板規范:
模板需要通過 Data Begin 和 Data End 來配置資料模板的開始和結束以識別每一個資料對應的開始行和結束行
示例代碼
示例配置
var setting = ExcelHelper.SettingFor<TestEntity>();
// ExcelSetting
setting.HasAuthor("WeihanLi")
.HasTitle("WeihanLi.Npoi test")
.HasDescription("WeihanLi.Npoi test")
.HasSubject("WeihanLi.Npoi test");
setting.HasSheetConfiguration(0, "SystemSettingsList", 1, true);
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(x => x.Enabled)
.HasColumnInputFormatter(val => "啟用".Equals(val))
.HasColumnOutputFormatter(v => v ? "啟用" : "禁用");
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 = "ddd1"
},
new TestEntity()
{
PKID=2,
SettingId = Guid.NewGuid(),
SettingName = "Setting2",
SettingValue = "Value2",
Enabled = true
},
};
var csvFilePath = $@"{tempDirPath}\test.csv";
entities.ToExcelFileByTemplate(
Path.Combine(ApplicationHelper.AppRoot, "Templates", "testTemplate.xlsx"),
ApplicationHelper.MapPath("templateTestEntities.xlsx"),
extraData: new
{
Author = "WeihanLi",
Title = "匯出結果"
}
);
匯出結果

More
為了方便使用,增加了一些方便的擴展方法:
public static int ToExcelFileByTemplate<TEntity>([NotNull]this IEnumerable<TEntity> entities, string templatePath, string excelPath, int sheetIndex = 0, object extraData = https://www.cnblogs.com/createwell/p/null);
public static int ToExcelFileByTemplate([NotNull]this IEnumerable entities, byte[] templateBytes, string excelPath, ExcelFormat excelFormat = ExcelFormat.Xls, int sheetIndex = 0, object extraData = null);
public static int ToExcelFileByTemplate([NotNull]this IEnumerable entities, IWorkbook templateWorkbook, string excelPath, int sheetIndex = 0, object extraData = null);
public static byte[] ToExcelBytesByTemplate([NotNull]this IEnumerable entities, string templatePath, int sheetIndex = 0, object extraData = null);
public static byte[] ToExcelBytesByTemplate([NotNull]this IEnumerable entities, byte[] templateBytes, ExcelFormat excelFormat = ExcelFormat.Xls, int sheetIndex = 0, object extraData = null);
public static byte[] ToExcelBytesByTemplate([NotNull]this IEnumerable entities, Stream templateStream, ExcelFormat excelFormat = ExcelFormat.Xls, int sheetIndex = 0, object extraData = null);
public static byte[] ToExcelBytesByTemplate([NotNull]this IEnumerable entities, IWorkbook templateWorkbook, int sheetIndex = 0, object extraData = null);
public static byte[] ToExcelBytesByTemplate([NotNull]this IEnumerable entities, ISheet templateSheet, object extraData = null);
Reference
- https://github.com/WeihanLi/WeihanLi.Npoi
- https://github.com/WeihanLi/WeihanLi.Npoi/blob/917e8fb798e9cbae52d121a7d593e37639870911/samples/DotNetCoreSample/Program.cs#L94
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/60066.html
標籤:其他
上一篇:單機軟體如何網路化部署?
下一篇:c語言實作迭代器iterator
