您的位置:首页 > 编程语言 > C#

C#获取单元格值(使用NPOI插件)

2016-04-05 10:48 651 查看
/// <summary>
/// 获取单元格的值
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public static object GetCellValue(ICell item)
{
if (item == null)
{
return string.Empty;
}
switch (item.CellType)
{
case CellType.Boolean:
return item.BooleanCellValue;

case CellType.Error:
return ErrorEval.GetText(item.ErrorCellValue);

case CellType.Formula:
switch (item.CachedFormulaResultType)
{
case CellType.Boolean:
return item.BooleanCellValue;

case CellType.Error:
return ErrorEval.GetText(item.ErrorCellValue);

case CellType.Numeric:
if (DateUtil.IsCellDateFormatted(item))
{
return item.DateCellValue.ToString("yyyy-MM-dd");
}
else
{
return item.NumericCellValue;
}
case CellType.String:
string str = item.StringCellValue;
if (!string.IsNullOrEmpty(str))
{
return str.ToString();
}
else
{
return string.Empty;
}
case CellType.Unknown:
case CellType.Blank:
default:
return string.Empty;
}
case CellType.Numeric:
if (DateUtil.IsCellDateFormatted(item))
{
return item.DateCellValue.ToString("yyyy-MM-dd");
}
else
{
return item.NumericCellValue;
}
case CellType.String:
string strValue = item.StringCellValue;
return strValue.ToString().Trim();

case CellType.Unknown:
case CellType.Blank:
default:
return string.Empty;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: