WeihanLi.Npoi輸出Excelテンプレートに従って
イントロ
複数行にデータ対応を行うことができない場合は、各行の比較的単純なエクスポートデータのためのより適切な輸出元の道、カスタマイズの比較的高いものの列データ、彼は、テンプレート、1.8に基づいてエクスポートをサポートしたかったので。テンプレートのエクスポート機能に応じて導入されたバージョン0
使用例
サンプルテンプレート
計画テンプレートは、3種類のデータを持つことができます。
- グローバル:あなたは、グローバルパラメータとして、デフォルトのパラメータ形式のパラメータを指定することができたときに導出された
$(Global:PropName)
フォーマットを - ヘッダー:対応する属性の表示設定、デフォルトの属性名、形式のデフォルトパラメータの名前:
$(Header:PropName)
- データ:属性値のデフォルトのパラメータ形式に対応したデータ:
$(Data:PropName)
(バージョン1.8.2でサポートされている最初からデフォルトのテンプレートパラメータ形式TemplateHelper.ConfigureTemplateOptions
定義方法から):
- グローバルパラメータ:
$(Global:{0})
- ヘッダパラメータ:
$(Header:{0})
- データパラメータ:
$(Data:{0})
- データが開始します:
<Data>
- データ終了:
</Data>
テンプレートの仕様:
テンプレートでデータテンプレートを設定するために必要とデータエンドデータが始まりとラインをスタートに対応し、エンディングの各データの終了を識別するために開始します
サンプルコード
サンプルの構成
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 = "导出结果"
}
);
エクスポート結果
もっと
使いやすさのために、いくつかの便利な拡張メソッドを追加します。
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);