1

Resolved

GetValue<decimal>() won't convert strings

description

ExcelWorksheet.GetValue<decimal>() always returns 0 for a cell that contains a number stored as a string. The GetTypedValue() method has no case for this conversion, and the final else returns a type's default value. GetTypedValue() looks more complicated than it needs to be as most of its functionality can be achieved with something like:
convertedValue= (T) Convert.ChangeType(v, typeof (T));
where v is the value before conversion.

I suggest that the GetTypedValue method should be simplified. I'll start working on it this weekend if noone beats me to it.

comments

ted_250 wrote Jul 29, 2012 at 10:25 PM

I've added a fix for compliance with this issue.

As patrick1 suggests, the GetTypedValue method could be simplified, but I'm not sure how well that would perform over all since the existing cases exist for good reasons (or we should assume that).

I've replaced the return of default(T) with the suggested conversion call without replacing the existing special cases. I also added an exception handler around the new code returns the same default(T) if the new line fails to find an adequate conversion.

I also added a test case for several scenarios and tested that the initial problem has been addressed. These new test now pass with the change.

Ted