Intro
The original export method is more suitable for simple export. Each data is in one row. Although the data column has a high degree of customization, if one data corresponds to more than one row, it can't be done. So we want to support export according to template. In version 1.8.0, the function of export according to template is introduced
Use example
Sample template
There are three types of data for template planning:
- Global: one is to specify some parameters when exporting. As a global parameter, the default parameter format is $(Global:PropName)
- Header: display name of the configured corresponding attribute. The default is the attribute name. The default parameter format is $(Header:PropName)
- Data: property value of corresponding data, default parameter format: $(Data:PropName)
Default template parameter format (since version 1.8.2, it is supported to customize through the TemplateHelper.ConfigureTemplateOptions method):
- Global parameter: $(Global:{0})
- Header parameter: $(Header:{0})
- Data parameter: $(Data:{0})
- Data Begin: <Data>
- Data End: </Data>
Template specification:
Templates need to configure the start and end of data templates through Data Begin and Data End to identify the start and end rows corresponding to each data
Sample code
Sample configuration
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 => "Enable".Equals(val)) .HasColumnOutputFormatter(v => v ? "Enable" : "Prohibit"); setting.Property("HiddenProp") .HasOutputFormatter((entity, val) => $"HiddenProp_{entity.PKID}"); setting.Property(_ => _.PKID).Ignored(); setting.Property(_ => _.UpdatedBy).Ignored(); setting.Property(_ => _.UpdatedTime).Ignored();
Export sample code from template:
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 = "Export results" } );
Export results
More
In order to facilitate use, some convenient extension methods are added:
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);