I have a working ExcelPackage function in an MVC 5 application where I can successfully output a strongly-typed model to a new Excel file.
I have a case where a spec
It comes down to how confident you are that the source excel tables are "properly" formatted. By that I mean are they stored as proper dates (i.e. numeric) or could you have the common excel problem of "numbers stored as string".
If the data is generally clean then you could avoid much of the casting back and forth you are doing to strings and dates by checking their types. Even this is not completely straight forward since Epplus likes to do its own interpretation when importing dates.
Take a look at this table (focus on col A):
Rows 1 - 4 have "properly" formatted data. That means that the dates and time are stored in excel as doubles. Rows 5-8 are "badly" formatted - numbers (and date/time) stored as string. If you run this:
var workbook = pck.Workbook;
var worksheet = workbook.Worksheets.First();
var cells = worksheet.Cells;
foreach (var cell in cells)
Console.WriteLine($"{{Cell: {cell.Address}, Display: {cell.Text}, Value: {cell.Value}, Type: {cell.Value.GetType()}}}");
You get this in the output:
{Cell: A1, Display: 11:33:00 AM, Value: 0.48125, Type: System.Double}
{Cell: A2, Display: 1/1/2016, Value: 1/1/2016 12:00:00 AM, Type: System.DateTime}
{Cell: A3, Display: 1/1/16 11:33 AM, Value: 42370.48125, Type: System.Double}
{Cell: A4, Display: 1264, Value: 1264, Type: System.Double}
{Cell: A5, Display: 11:33:00 AM, Value: 11:33:00 AM, Type: System.String}
{Cell: A6, Display: 1/1/2016, Value: 1/1/2016, Type: System.String}
{Cell: A7, Display: 1/1/2016 11:33:00 AM, Value: 1/1/2016 11:33:00 AM, Type: System.String}
{Cell: A8, Display: 1264, Value: 1264, Type: System.String}
Since dates and times are just numbers technically (the integer part is the date and the decimal is the time) this gives you a way to convert or separate them. A time double of 0.0 means 00:00:00. Note that row 3 shows as a System.DateTime
because, like I said, Epplus just happens to recognize that Excel Style as such but the others are Doubles
.
So, you could use a Type
check and avoid much of the string conversion and comparison. Again, if you are worried about badly formatted data then your approach is probably as good as any. I would suggest making the value Convert.ToDateTime("12/30/1899"))
a constant somewhere before the for loops rather then recreating it every time you increment to save on some cpu cycles.