Read "date" from excel file using Interop Excel.Application

excel-interop, interop, vb.net

Solution

You found your answer but some background might help.

Excel holds dates and times as numbers. If a user types 31-May-2012 into a cell, Excel recognises this as a date so stores the value as 41060 and sets the number format to "dd-mmm-yyyy". If a user types 41060 into a cell and sets the number format to "dd-mmm-yyyy", the value will be displayed as 31-May-2012. Once the data entry is finished, Excel does not record that the first 41060 was entered as a date and the second as a number.

On a PC, the integer part of a date is days since the year 1900 (On a Mac the year 1904 is used) and the decimal part is:

time in seconds
---------------
seconds in day

So 41060.25 represents: 31 May 2012 6:00:00

When reading data via the Excel InterOp, I suggest you get `.NumberFormat` as well as `.Value` if you do not know the type of the data. `.Text` might also be useful; it gives the value the user sees.

Warning: if you do get `.NumberFormat`, the formats used by Excel are slightly different from the formats used by VBA's Format() and from VB.Net's Format().

Visit How to use dates and times in Excel for more detail on Excel dates.

Problem

I am reading and fetching information from excel file using interop but I faced one problem that when I read date from my excel file. 02/05/2012; It gives 41060. How can I read proper date value or why it returns 41060?

Original source