23

WeihanLi.Npoi 支持 ShadowProperty 了

 4 years ago
source link: http://mp.weixin.qq.com/s?__biz=MzAxMjE2NTMxMw%3D%3D&%3Bmid=2456606025&%3Bidx=1&%3Bsn=65f72dffc57fad884fe1d817857872b4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

WeihanLi.Npoi 支持 ShadowProperty

Intro

在 EF 里有个 ShadowProperty (阴影属性/影子属性)的概念,你可以通过 FluentAPI 的方式来定义一个不在 .NET model 里定义的属性,只能通过 EF 里的 ChangeTracker 来操作这种属性。

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

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

使用示例

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

导出效果如下:

2UfMrqI.png!web

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

More

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

Reference

  • https://docs.microsoft.com/en-us/ef/core/modeling/shadow-properties

  • https://docs.microsoft.com/zh-cn/ef/core/modeling/shadow-properties

  • https://github.com/WeihanLi/WeihanLi.Npoi/issues/51

  • https://github.com/WeihanLi/WeihanLi.Npoi


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK