I am reading in values from an XLSX Worksheet and putting them into a Data table.
My problem is that I cannot seem to infer the Datatype of the cell so that I can set the Data table’s Column type.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Excel has a much looser approach to typing data than C#. For example, numeric values are always doubles. If you use the
Valueproperty of the range object (get_Value()in interop, if I recall correctly), Excel will possibly return a (boxed)decimalorDateTimeif the cell’s contents and formatting are consistent with those data types.If you use the
Value2property, money and date values will be returned as boxed doubles. (The numbers are all stored internally as doubles, so, because Excel doesn’t need to test for whether to convert them, Value2 is slightly faster.)So, you could check the type of each value in a column, and choose your data column’s type accordingly. If you want to test whether a column could have a more specific numeric type (float, int, short, byte, etc.), then you’ll also need to check the range of values in that column, and test whether they are integers.