在excel文档中,数字、日期类型数据在编辑后会变成numeric格式
导致在读取过程中,如果按照原来格式读取会遇到问题
故添加以下函数进行判断
/// <summary>
/// 获取字符串格式的值
/// </summary>
/// <param name="cell"></param>
/// <returns></returns>
private string GetCellStringValue(ICell cell)
{
try
{
if (cell == null || cell.CellType == CellType.Blank)
{
return null;
}
string result;
switch (cell.CellType)
{
case CellType.Numeric:
result = cell.NumericCellValue.ToString(CultureInfo.InvariantCulture);
break;
default:
result = cell.StringCellValue?.Trim();
break;
}
return result;
}
catch
{
return null;
}
}
/// <summary>
/// 获取日期格式的值
/// </summary>
/// <param name="cell"></param>
/// <returns></returns>
private DateTime? GetCellDateTimeValue(ICell cell)
{
try
{
if (cell == null || cell.CellType == CellType.Blank)
{
return null;
}
DateTime? result;
switch (cell.CellType)
{
case CellType.Numeric:
result = cell.DateCellValue;
break;
case CellType.String:
result = DateTime.Parse(cell.StringCellValue);
break;
default:
result = null;
break;
}
return result;
}
catch
{
return null;
}
}
/// <summary>
/// 获取数字格式的值
/// </summary>
/// <param name="cell"></param>
/// <returns></returns>
private double? GetCellNumericValue(ICell cell)
{
try
{
if (cell == null || cell.CellType == CellType.Blank)
{
return null;
}
double? result;
switch (cell.CellType)
{
case CellType.Numeric:
result = cell.NumericCellValue;
break;
case CellType.String:
result = double.Parse(cell.StringCellValue);
break;
default:
result = null;
break;
}
return result;
}
catch
{
return null;
}
}