Sunday, 15 April 2012

c# - Excel Date Column not Correctly read -



c# - Excel Date Column not Correctly read -

i reading date column excel such

var y = datetime.fromoadate(getexcelcellvalue(range, rowindex, datecolumnnumber)); private dynamic getexcelcellvalue(microsoft.office.interop.excel.range range, short? row, int col) { var cell = (range.cells[row, col] microsoft.office.interop.excel.range); homecoming cell == null ? null : cell.value2; }

but error exception =

the best overloaded method match 'system.datetime.fromoadate(double)' has invalid arguments"

and when used

var x = datetime.fromoadate(convert.todouble(getexcelcellvalue(range, rowindex, datecolumnnumber)));

i date value of 30/12/1899

any ideas?

per microsoft specification datetime.fromoadate() method expects double type parameter can seen below

public static datetime fromoadate(double d)

and getexcelcellvalue() method doesn't homecoming double type , error.

in sec case, explicitly casting double , don't error because of that.

you can alter getexcelcellvalue method homecoming nullable double instead.

using microsoft.office.interop.excel; private double? getexcelcellvalue(range range, short? row, int col) { var cell = (range.cells[row, col] range); //using null coalescing operator. homecoming cell ?? cell.value2; }

c# excel datetime

No comments:

Post a Comment