使用EPPlus时如何设置列类型 [英] How to set Column Type when using EPPlus
问题描述
我正在使用 EPPlus
生成 Excel
文件,在DAL中我正在填充 DataTable
,将数据填充到表中,然后将表传递给Presentation Layer。在此,我使用 LoadFromDataTable()
方法生成 Excel
文件。
I'm using EPPlus
to generate Excel
files, in DAL I'm populating DataTable
, filling data into table, and passing table to Presentation Layer. From there I'm using LoadFromDataTable()
method to generate Excel
file.
一切正常,除了我想将列的类型之一设置为 Date
。我尝试将 DataTable
的列类型设置为 Date
,然后传递 DataTable
到Presentation Layer,但似乎 EPPlus
要么忽略它,要么不认识,因为当我打开时会生成 Excel
文件,单元格的类型为 Number
。
Everything works fine, except that I want to set one of the column's type to Date
. I tried to set Column type of my DataTable
toDate
and than pass DataTable
to Presentation Layer, but it seems EPPlus
either, ignored it, or didn't recognize, because when I'm opening generated Excel
file, cell's type is Number
.
如果我手动设置单元格格式并将类型设置为 Date
, Excel
显示正确的日期。那么如何实现呢?
If I manually Format Cells and set Type to Date
, Excel
shows correct dates. So how can I achieve this ?
推荐答案
您确实需要DataTable列具有正确的类型,但是还需要修改列或单元格的Style.Numberformat.Format属性。
You do need the DataTable column to have the right type but you also need to modify the column or cell's Style.Numberformat.Format property.
假设您有一个名为的
: ExcelWorksheet
ws
Say you have an ExcelWorksheet
named ws
:
ws.Column(1).Style.Numberformat.Format = "yyyy-mm-dd";
//OR "yyyy-mm-dd h:mm" if you want to include the time!
这篇关于使用EPPlus时如何设置列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!