Search code examples
c#exceldatelinq-to-excel

Having problems reading an Excel spreadsheet using LinqToExcel, when dates are involved


I've got an Excel spreadsheet from our HR department that needs to be loaded into a database. One column has dates in it. I've searched here on SO and found 2 answers (first one and the second one) which I thought might address my problem, but neither did.

First I tried doing a simple open of the spreadsheet:

var excel = new ExcelQueryFactory(@"D:\Projects\LRAT\Example of SPO report 08-25-17 - Copy.xlsx");

I left the first row alone. In the class I defined to match the spreadsheet I defined the property for the dates originally like this:

[ExcelColumn("Date Current Step Filed")]
[DefaultValue("")]
public string DateStepsFiled { get; set; }

This worked, so longer as there were multiple dates in the column. But when there was just 1 date, LinqToExcel interpreted it with values like this: 39758. Clearly, not at date. Or at least not one in mm/dd/yyyy format.

In reading what I did I got the impression that the first row, commonly the header row, influenced how LinqToExcel would process the spreadsheet. I don't quite get that because I thought that LinqToExcel ignored the first row anyway, but I decided to change the header for this column to today's date and I also changed the definition of the DateStepsFiled to this:

[ExcelColumn("Date Current Step Filed")]
[DefaultValue("")]
public DateTime[] DateStepsFiled { get; set; }

This was much worse, as LinqToExcel now defined all of the DateStepsFiled as null. So, how do I resolve this problem?


Solution

  • it should be

    public DateTime DateStepsFiled { get; set; }
    

    instead of

    public DateTime[] DateStepsFiled { get; set; }