How do you convert Excel Date type to DateTime in .NET?
Posted on 2007-07-25
am using the Office Primary Interop Assemblies (automation) to access an
Excel file from .NET and get cell contents. I'm using C#.
When I get the cell contents that contains an Excel Date value, I am having
trouble converting from the object variable type to DateTime.
The following error is displayed:
The specified cast is invalid.
Here's the code with error:
Range rng = (Range)oSheet.get_Range("A4", Type.Missing);
System.Object myvalue = rng.Cells.Value2; //Assign it to an object var
DateTime dt = (DateTime)myvalue;
So how do I cast the retrieved cell contents to .NET DateTime?