Npoi ICell.DateCellValue return NullReferenceException
Npoi ICell.DateCellValue return NullReferenceException
我有这个 Excel 列:
格式化为日期数据格式:
如果我尝试读取 DateTime 值,我会得到 NullReferenceException
。
您知道这里出了什么问题以及如何解决吗?是否有可能以某种方式将数字转换为 DateTime?比如31/12/9999我换成数字格式就是2958465
ICell into string extension
public static class NpoiExtension
{
public static string GetStringValue(this ICell cell)
{
switch (cell.CellType)
{
case CellType.Numeric:
if (DateUtil.IsCellDateFormatted(cell))
{
try
{
return cell.DateCellValue.ToString();
}
catch (NullReferenceException)
{
//
//var prevCulture = Thread.CurrentThread.CurrentCulture;
//CultureInfo customCulture = new CultureInfo("en-GB", false);
//Thread.CurrentThread.CurrentCulture = customCulture;
string dateOutput = cell.DateCellValue.ToString();
//Thread.CurrentThread.CurrentCulture = prevCulture;
return dateOutput;
}
}
else
{
return cell.NumericCellValue.ToString();
}
case CellType.String:
return cell.StringCellValue;
case CellType.Boolean:
return cell.BooleanCellValue.ToString();
default:
return string.Empty;
}
}
}
我在这里找到了解决方案How do I convert an Excel serial date number to a .NET DateTime?所以我将它添加到我的方案中。
public static string GetStringValue(this ICell cell)
{
switch (cell.CellType)
{
case CellType.Numeric:
if (DateUtil.IsCellDateFormatted(cell))
{
try
{
return cell.DateCellValue.ToString();
}
catch (NullReferenceException)
{
return DateTime.FromOADate(cell.NumericCellValue).ToString();
}
}
return cell.NumericCellValue.ToString();
case CellType.String:
return cell.StringCellValue;
case CellType.Boolean:
return cell.BooleanCellValue.ToString();
default:
return string.Empty;
}
}
每当单元格定义为数字时获取日期时间的另一个简洁选项是获取单元格的数值:
DateTime.FromOADate(cell.NumericCellValue);
一个完整的例子:
private ICell GetCellValue(string position)
{
var cr = new CellReference(position);
var row = m_Sheet.GetRow(cr.Row);
return row.GetCell(cr.Col);
}
public DateTime? GetCellDateValue(string position)
{
ICell cellValue = GetCellValue(position);
if (cellValue == null)
{
// Cell doesn't have any value
return null;
}
if (cellValue.CellType == CellType.Numeric)
{
return DateTime.FromOADate(cellValue.NumericCellValue);
}
return cellValue.DateCellValue;
}
我有这个 Excel 列:
格式化为日期数据格式:
如果我尝试读取 DateTime 值,我会得到 NullReferenceException
。
您知道这里出了什么问题以及如何解决吗?是否有可能以某种方式将数字转换为 DateTime?比如31/12/9999我换成数字格式就是2958465
ICell into string extension
public static class NpoiExtension
{
public static string GetStringValue(this ICell cell)
{
switch (cell.CellType)
{
case CellType.Numeric:
if (DateUtil.IsCellDateFormatted(cell))
{
try
{
return cell.DateCellValue.ToString();
}
catch (NullReferenceException)
{
//
//var prevCulture = Thread.CurrentThread.CurrentCulture;
//CultureInfo customCulture = new CultureInfo("en-GB", false);
//Thread.CurrentThread.CurrentCulture = customCulture;
string dateOutput = cell.DateCellValue.ToString();
//Thread.CurrentThread.CurrentCulture = prevCulture;
return dateOutput;
}
}
else
{
return cell.NumericCellValue.ToString();
}
case CellType.String:
return cell.StringCellValue;
case CellType.Boolean:
return cell.BooleanCellValue.ToString();
default:
return string.Empty;
}
}
}
我在这里找到了解决方案How do I convert an Excel serial date number to a .NET DateTime?所以我将它添加到我的方案中。
public static string GetStringValue(this ICell cell)
{
switch (cell.CellType)
{
case CellType.Numeric:
if (DateUtil.IsCellDateFormatted(cell))
{
try
{
return cell.DateCellValue.ToString();
}
catch (NullReferenceException)
{
return DateTime.FromOADate(cell.NumericCellValue).ToString();
}
}
return cell.NumericCellValue.ToString();
case CellType.String:
return cell.StringCellValue;
case CellType.Boolean:
return cell.BooleanCellValue.ToString();
default:
return string.Empty;
}
}
每当单元格定义为数字时获取日期时间的另一个简洁选项是获取单元格的数值:
DateTime.FromOADate(cell.NumericCellValue);
一个完整的例子:
private ICell GetCellValue(string position)
{
var cr = new CellReference(position);
var row = m_Sheet.GetRow(cr.Row);
return row.GetCell(cr.Col);
}
public DateTime? GetCellDateValue(string position)
{
ICell cellValue = GetCellValue(position);
if (cellValue == null)
{
// Cell doesn't have any value
return null;
}
if (cellValue.CellType == CellType.Numeric)
{
return DateTime.FromOADate(cellValue.NumericCellValue);
}
return cellValue.DateCellValue;
}