Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Column Type when using EPPlus

Tags:

.net

excel

epplus

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.

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.

If I manually Format Cells and set Type to Date, Excel shows correct dates. So how can I achieve this ?

like image 835
Michael Avatar asked Mar 25 '12 10:03

Michael


People also ask

How do I change the date format on EPPlus?

Format = "yyyy-mm-dd"; ws. Cells[3, 1]. Value = new DateTime(2014,10,5); ws.

What is the use of EPPlus?

EPPlus is a very helpful open-source 3rd party DLL for writing data to excel. EPPlus supports multiple properties of spreadsheets like cell ranges, cell styling, charts, pictures, shapes, comments, tables, protection, encryption, pivot tables, data validation, conditional formatting, formula calculation, etc.

What is EPPlus library?

What is EPPlus? A library to manage Excel spreadsheets. EPPlus is a . NET library, which reads and writes Excel 2007/2010 or higher files, using Open Office XML format. It supports .


1 Answers

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.

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! 
like image 183
banging Avatar answered Sep 23 '22 13:09

banging