日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

​WeihanLi.Npoi 根据模板导出Excel

發布時間:2023/12/4 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ​WeihanLi.Npoi 根据模板导出Excel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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}, }; 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 = null); public static int ToExcelFileByTemplate<TEntity>([NotNull]this IEnumerable<TEntity> entities, byte[] templateBytes, string excelPath, ExcelFormat excelFormat = ExcelFormat.Xls, int sheetIndex = 0, object extraData = null); public static int ToExcelFileByTemplate<TEntity>([NotNull]this IEnumerable<TEntity> entities, IWorkbook templateWorkbook, string excelPath, int sheetIndex = 0, object extraData = null); public static byte[] ToExcelBytesByTemplate<TEntity>([NotNull]this IEnumerable<TEntity> entities, string templatePath, int sheetIndex = 0, object extraData = null); public static byte[] ToExcelBytesByTemplate<TEntity>([NotNull]this IEnumerable<TEntity> entities, byte[] templateBytes, ExcelFormat excelFormat = ExcelFormat.Xls, int sheetIndex = 0, object extraData = null); public static byte[] ToExcelBytesByTemplate<TEntity>([NotNull]this IEnumerable<TEntity> entities, Stream templateStream, ExcelFormat excelFormat = ExcelFormat.Xls, int sheetIndex = 0, object extraData = null); public static byte[] ToExcelBytesByTemplate<TEntity>([NotNull]this IEnumerable<TEntity> entities, IWorkbook templateWorkbook, int sheetIndex = 0, object extraData = null); public static byte[] ToExcelBytesByTemplate<TEntity>([NotNull]this IEnumerable<TEntity> 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

總結

以上是生活随笔為你收集整理的​WeihanLi.Npoi 根据模板导出Excel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。