您的位置:首页 > 其它

Excel读取获取数据必须判断数据类型

2018-01-03 10:24 501 查看
如之前读取Excel的各种方法,在获取单元格数据的时候要注意,不要直接ToString(),如果直接ToString遇到数字的时候,比如'0.9',会变成'.9'。

在获取数据之前判断数据类型,并根据相应的数据类型去获取,才不会出问题。

private static string GetCellValue(ICell cell)
{
object value = null;
try
{
if (cell.CellType != CellType.Blank)
{
switch (cell.CellType)
{
case CellType.Numeric:
// Date comes here
if (DateUtil.IsCellDateFormatted(cell))
{
value = cell.DateCellValue;
}
else
{
// Numeric type
value = cell.NumericCellValue;
}
break;
case CellType.Boolean:
// Boolean type
value = cell.BooleanCellValue;
break;
case CellType.Formula:
value = cell.CellFormula;
break;
default:
// String type
value = cell.StringCellValue;
break;
}
}
}
catch (Exception)
{
value = "";
}

if (value == null)
return "";
else
return value.ToString();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐