Epplus: How to make “LoadFromCollection” use [DisplayFormat()] attributes in model

耗尽温柔 提交于 2019-12-22 14:57:46

问题


I have an ActionResult (in an MVC 5 website) that successfully uses Epplus to export a data set to an Excel document.

In the ActionResult code block, I use code like this to format the column as a short date. WIthout this code, the date column appears as int values.

// format date columns as date
worksheet.Column(6).Style.Numberformat.Format =
    DateTimeFormatInfo.CurrentInfo.ShortDatePattern;

However, in the Model, I already have this defined as an attribute. It works great on the View page, but doesn't format the column as a short date -- hence, the code shown above.

[Column(TypeName = "date")]
[DisplayName("Exit Date")]
**[DisplayFormat(DataFormatString = "{0:d}")]**
public DateTime ExitDate { get; set; }

To provide further context, I load my worksheet from a collection.

worksheet.Cells["A5"].LoadFromCollection(Collection: exportQuery, PrintHeaders: true);

Is there a way that I can extend LoadFromCollection so that that the worksheet not only loads the Collection content, but also the formatting attributes that exist in the model? The "DisplayName" attributes are collected, so is there a way to also collect "DisplayFormat" attributes without having to write separate code?


回答1:


EPPlus unfortunately didn't implement support for DisplayFormat.

The library is open source, so you can find the full implementation of LoadFromCollection and its overloads on codeplex

To answer you question: I'm afraid you will have to write separate code for this.



来源:https://stackoverflow.com/questions/34209678/epplus-how-to-make-loadfromcollection-use-displayformat-attributes-in-mod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!