I’m working on VS2008 .NET3.5 with a Office 2003 doc (.xls). I’m using Microsoft.Office.Interop.Excel to access to a document.
This works fine for my purpose but fails whenever I try to get a Date.
The way I’m using is the most common:
object date= xlWorkSheet.get_Range("E" + i, "E" + i).Value2;
double doubleDate = double.Parse(date);
DateTime finallyDate = DateTime.FromOADate(doubleDate);
The date I have stored is 01/12/1961 (in Italian means first december and if I open excel it tolds me 1 december 1961).
When I run my app it happens that the value of the double become 15011.0 and when the finallyDate value is 2/4/1941 that’s not right!
How can I solve this problem? Is there any way to convert (also manually) that 15011 number?
Thank you!!
Get the Value property instead of Value2, then you will be able to work with a Date object. You may need to cast it as (DateTime).
What you get instead with Value2 is the floating point value of the Date.
For example, check out my spreadsheet below, where A1 contains a date:
Then in Excel, I add my reference to Microsoft.Office.Interop.Excel, and grab the Value and Value2 properties like so:
I get this output:
Interestingly, if you try this in a .Net 4.5 application, it still works but the type of
varis resolved as dynamic forrangeAsValueandrangeAsValue2, and you lose your intellisense.