views:

336

answers:

2

I am parsing an Excel file and am having difficulty reading in the value of a cell that has a custom cell format of dd mmm yy. The value in the cell in question is 29 Oct 09 (cell B25). When I run

String arrive = Convert.ToString(_worksheets["GENERAL"].get_Range("B25", Type.Missing).Value2);

I get "40114" as the cell's value.

Then when I try

DateTime arrive = Convert.ToDateTime(_worksheets["GENERAL"].get_Range("B25", Type.Missing).Value2);

I get an error stating "When casting from a number the value must be a number less than infinity."

Any thoughts ideas would be greatly appreciated.

Thanks.

A: 

Seems like you want the DateTime.ParseExact method, since this is a non-standard format you're working with, as far as .NET is concerned.

Try the following:

var dateTimeStr = _worksheets["GENERAL"].get_Range("B25", Type.Missing).Value2);
var dateTime = DateTime.ParseExact(dateTimeStr, "dd MMM yy");

The MSDN Custom Date and Time Frormat Strings page may also be a good reference to bookmark.

Noldorin
Without wishing to be difficult, that's not going to work terribly well converting 40114... I agree that if he could guaranteeably pull the value in the specified format it would work
Murph
Thanks for the quick response. When I tried the suggestion I received an error stating that ParseExact takes 2 arguments. I guess this is where I state this is the first time I have ever coded anything in C# and apologize profusely for my noobness.
yoJordan
+2  A: 

That random number is an OLE date.

You probably want the DateTime.FromOADate method. Have a look at the reference on MSDN.

Ant
Ant -- that did it man. Thanks a ton.
yoJordan
Nice - glad to hear it!
Ant