代码之家  ›  专栏  ›  技术社区  ›  Muflix

Npoi ICell。DateCellValue返回NullReferenceException

  •  1
  • Muflix  · 技术社区  · 6 年前

    我有一个Excel列:

    enter image description here

    格式为日期数据格式:

    enter image description here

    我得到了 NullReferenceException 如果我尝试读取DateTime值。

    enter image description here

    你知道这里出了什么问题,怎么解决吗?有没有可能以某种方式将数字转换成日期时间?例如,当我更改为数字格式时,31/12/9999是2958465。

    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)
                        {
                            // https://stackoverflow.com/questions/15040567/c-xlsx-date-cell-import-to-datatable-by-npoi-2-0
                            //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;
            }
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  12
  •   krlzlx    6 年前

    我在这里找到了解决方案 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;
        }
    }
    
        2
  •  1
  •   Aharon Ohayon    4 年前

    当单元格被定义为数字时,另一个获取日期时间的简洁选项是获取单元格的数值:

    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;
            }