欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WeihanLi.Npoi 1.7.0 更新

程序员文章站 2022-03-25 18:01:39
WeihanLi.Npoi 1.7.0 版本,增加了 `ColumnInputFormatter`/`ColumnOutputFormatter`,又进一步增强了导入导出的灵活性 ......

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);

导出结果:

WeihanLi.Npoi 1.7.0 更新

导入结果:

WeihanLi.Npoi 1.7.0 更新

WeihanLi.Npoi 1.7.0 更新

more

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

columninputformatter 结合之前的 inputformatter/outputformatter 能够帮助你更灵活的导入导出,快来试试吧~~

reference