I have an excel (xlsx 2010) document that contains nothing but two columns, one for DateTime, and one for a value, of the form:
DateTime Value
01/09/2012 00:00:00 1.23456
02/09/2012 00:05:00 1.23457
03/09/2012 00:10:00 1.23458
04/09/2012 00:15:00 1.23459
The date time column is set to a custom format (dd/mm/yyyy hh:mm:ss), and all the Times are exacally on either the 5 or 10 minute mark, the seconds are always 00.
However when I read this using:
connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;", filePath);
OleDbCommand sqlComm = new OleDbCommand("SELECT * FROM [Sheet1$]", new OleDbConnection(connectionString));
OleDbDataReader r = sqlComm.ExecuteReader();
The first two DateTimes are read correctly, but there after all of the DateTimes are one second less than what the file actually contains eg:
00:15:00 00:14:59
00:20:00 00:19:59
Has anyone got any idea why? Or what I can do to correct it, short of checking them manually and rounding up to the nearest minute (a viable option in this case – I think)
PS. I’ve cut and pasted the relevant lines of code for the sake of brevity, so the above code sample may not technically work, but in the real app it does read the files
Converting comment to answer
After a major revamp and a steep learning curve I can confirm that using the interop libraries read the file correctly.
See below for sample code, based on other posts across the web