Excel单元格中的值类型

时间:2022-08-04 08:24:35

I'm using C# to read an excel sheet and use the following code to read the values:

我正在使用C#读取excel表并使用以下代码读取值:

range = xlWorkSheet.UsedRange;
object value = (range.Cells[rCnt, cCnt] as Excel.Range).get_Value(Type.Missing);

In case an integer number appears there (for example 2), then it shows me as float: 2.0

如果出现整数(例如2),那么它显示为float:2.0

How could I know if it's an float or integer there?

我怎么知道它是浮点数还是整数?

More than that, how could I know if a string is stored in the cell, or integer or float?

更重要的是,我怎么知道字符串是存储在单元格中,还是整数或浮点数?

Thanks Moti

1 个解决方案

#1


1  

I am not sure is Excel makes difference between integers and floats -- you can enter 1 to cell and then move decimal point making it look like 1.00. To figure out whether it is integer or not you may try the following:

我不确定Excel是否会在整数和浮点数之间产生差异 - 您可以在单元格中输入1,然后移动小数点使其看起来像1.00。要确定它是否为整数,您可以尝试以下方法:

var r;
if (Int32.TryParse(value, out r))
{
    // r now stores int value
}

float.TryParse() is also there :)

float.TryParse()也在那里:)

#1


1  

I am not sure is Excel makes difference between integers and floats -- you can enter 1 to cell and then move decimal point making it look like 1.00. To figure out whether it is integer or not you may try the following:

我不确定Excel是否会在整数和浮点数之间产生差异 - 您可以在单元格中输入1,然后移动小数点使其看起来像1.00。要确定它是否为整数,您可以尝试以下方法:

var r;
if (Int32.TryParse(value, out r))
{
    // r now stores int value
}

float.TryParse() is also there :)

float.TryParse()也在那里:)