码迷,mamicode.com
首页 > 其他好文 > 详细

WeihanLi.Npoi 1.7.0 更新

时间:2020-01-04 12:56:41      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:datetime   class   createdb   git   reference   pac   als   abstract   src   

WeihanLi.Npoi 1.7.0 更新介绍

Intro

昨天晚上发布了 WeihanLi.Npoi 1.7.0 版本,增加了 ColumnInputFormatter/ColumnOutputFormatter,又进一步增强了导入导出的灵活性,来看下面的示例

ColumnInputFormatter/ColumnOutputFormatter

示例 Model:

internal abstract class BaseEntity
{
    public int PKID { get; set; }
}

internal class TestEntity : BaseEntity
{
    public Guid SettingId { get; set; }

    public string SettingName { get; set; }

    public string DisplayName { get; set; }
    public string SettingValue { get; set; }

    public string CreatedBy { get; set; } = "liweihan";

    public DateTime CreatedTime { get; set; } = DateTime.Now;

    public string UpdatedBy { get; set; }

    public DateTime UpdatedTime { get; set; }

    public bool Enabled { get; set; }
}

示例配置:

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.HasFilter(0, 1).HasFreezePane(0, 1, 2, 1);

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 => "启用".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 path = $@"{tempDirPath}\test.xlsx";
entities.ToExcelFile(path);
var entitiesT0 = ExcelHelper.ToEntityList<TestEntity>(path);

导出结果:

技术图片

导入结果:

技术图片

技术图片

More

新版本发布,快来尝鲜吧~

ColumnInputFormatter 结合之前的 InputFormatter/OutputFormatter 能够帮助你更灵活的导入导出,快来试试吧~~

Reference

WeihanLi.Npoi 1.7.0 更新

标签:datetime   class   createdb   git   reference   pac   als   abstract   src   

原文地址:https://www.cnblogs.com/weihanli/p/weihanli-npoi-1_7_0_Released.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!