当前位置: 移动技术网 > IT编程>开发语言>c# > WeihanLi.Npoi 支持 ShadowProperty 了

WeihanLi.Npoi 支持 ShadowProperty 了

2019年12月13日  | 移动技术网IT编程  | 我要评论

weihanli.npoi 支持 shadowproperty

intro

在 ef 里有个 shadowproperty (阴影属性/影子属性)的概念,你可以通过 fluentapi 的方式来定义一个不在 .net model 里定义的属性,只能通过 ef 里的 change tracker 来操作这种属性。

在导出 excel 的时候,可能希望导出的列并不是都定义好在我们的 model 中的,有的可能只是想增加一列导出某个属性中的嵌套属性之中的某一个属性值,或者我就是单纯的想多定义一列,而这个时候可能 model 是别的地方写死的,不方便改。

于是 weihanli.npoi 从 1.6.0 版本开始支持 shadowproperty ,将 ef 里的 shadowproperty 引入到 excel 导出里,目前来说 shadowproperty 是不可写的,读取的话也只是返回一个类型的默认值,不支持 changetracker,不支持改。

使用示例

来看一个简单使用示例:(示例来源于网友提出的这个issue: https://github.com/weihanli/weihanli.npoi/issues/51)

using system;
using system.collections.generic;
using system.io;
using weihanli.npoi;

namespace npoitest
{
    public class program
    {
        public static void main(string[] args)
        {
            var settings = excelhelper.settingfor<testentity>();
            settings.property(x => x.name)
                .hascolumnindex(0);
            // settings.property(x => x.userfields)
            //     .hasoutputformatter((entity, value) => $"{value[0].value},{value[2].value}")
            //     .hascolumntitle("姓名,工号")
            //     .hascolumnindex(1);
            settings.property(x=>x.userfields).ignored();
            settings.property("工号")
                .hasoutputformatter((entity,val)=> $"{entity.userfields[2].value}")
                 ;
            settings.property("部门")
                .hasoutputformatter((entity,val)=> $"{entity.userfields[1].value}")
                 ;

            var data = new list<testentity>()
            {
                new testentity()
                {
                    name = "xiaoming",
                    totalscore = 100,
                    userfields = new userfield[]
                    {
                        new userfield()
                        {
                            name = "姓名",
                            value = "xaioming",
                        },
                        new userfield()
                        {
                            name = "部门",
                            value = "1212"
                        },
                        new userfield()
                        {
                            name = "工号",
                            value = "121213131"
                        },
                    }
                }
            };
            data.toexcelfile($@"{directory.getcurrentdirectory()}\output.xls");
            console.writeline("complete.");
        }

        private class testentity
        {
            public string name { get; set; }

            public userfield[] userfields { get; set; }

            public int totalscore { get; set; }
        }

        private class userfield
        {
            public string fid { get; set; }
            public string name { get; set; }
            public string value { get; set; }
        }
    }
}

导出效果如下:

可以看到,我们为导出的 excel 增加在原本的 model 里没有定义的两列,借助于此,我们可以更灵活的定制要导出的内容

more

快来体验吧,欢迎反馈,欢迎 issue

reference

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网