Handle datetime display for excel columns

Using EPPlus package for exporting excel document(s)

LAI TOCA
2 min readMay 28, 2021
Photo from: https://www.syncfusion.com/blogs/post/6-easy-ways-to-export-data-to-excel-in-c-sharp.aspx

Background

Suppose we got below class object:

public class Customer
{
public string ID{ get; set; }
public string Sex { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public DateTime CreateDate { get; set; }
}

We would like to export above structure as below example of excel document.

Example of excel

We could use EPPlus package for .NET Core that help us dynamic creating excel document for just few lines of code block:

The key of how to dynamic was introduced method: LoadFromCollection that help us handle diverse class objects (Customer/Order…) and no need create additional template(s) anymore:)

So far everything would be perfect but the Column: CreateDate looks a little wire. The original “DateTime” for excel was treat as data type as number. But it was not easy for human to understand the meaning of the column.

The ideal here was we could found out the sequence number of data type “DateTime”. Then we could force to style the display format of those “DateTime” column(s) more human readable. The completed code example please refer to below Gist example.

After we adjust the style of the “DateTime” column(s). The content of excel should looks like below. If you got better ideal for handling this kind of cases, please fell free to write down under the bulletin board😊.

Human readable excel format for column: CreateDate

Reference

--

--

LAI TOCA

Coding for fun. (Either you are running for food or running for being food.)