Fill range. Value2 in Excel to interact with string

I am importing some values ​​from Excel sheets in a C # application. One column is mostly text, but can contain any values. I used the following:

Range range = ... // getting this from Excel, works fine
string myString = (string)range.Value2;

      

When a cell contains text, it works. But when it contains 123 for example, the debugger shows 123.0 for the range. Value2 and the conversion to string fails.

I wonder how to write the most general transform for all types of cells. At least string and integer can be floating point content.

+3


source to share


1 answer


I found a solution that may not be very pleasant, but it works:

myString = range.Value2 == null ? "" : range.Value2.ToString();

      



Maybe something better exists.

+3


source







All Articles