Epplus:如何制作"LoadFromCollection"在模型中使用[DisplayFormat()]属性 [英] Epplus: How to make "LoadFromCollection" use [DisplayFormat()] attributes in model

查看:346
本文介绍了Epplus:如何制作"LoadFromCollection"在模型中使用[DisplayFormat()]属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ActionResult(在MVC 5网站中),可以成功使用 Epplus 将数据集导出到Excel文档中.

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

在ActionResult代码块中,我使用类似这样的代码将列格式设置为短日期.如果没有此代码,则日期列将显示为int值.

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

有没有一种方法可以扩展LoadFromCollection,以便工作表不仅可以加载Collection内容,还可以加载模型中存在的格式设置属性?收集了"DisplayName"属性,是否有一种方法也可以收集"DisplayFormat"属性而无需编写单独的代码?

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?

推荐答案

EPPlus 没有实现对DisplayFormat的支持.

EPPlus unfortunately didn't implement support for DisplayFormat.

该库是开源的,因此您可以在LoadFromCollection的完整实现及其重载. ="nofollow"> codeplex

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.

这篇关于Epplus:如何制作"LoadFromCollection"在模型中使用[DisplayFormat()]属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆