【问题标题】:Excel time format using Epplus使用Epplus的Excel时间格式
【发布时间】:2019-08-01 17:54:59
【问题描述】:

我有一个 Excel 文档,我尝试将其导入我的系统。 (.net core 2.2 和 EPPlus v4.5.3.1)

09:57:32等excel数据和自定义单元格格式[hh]:mm:ss

private TimeSpan? GetRequiredTimeFromRowOrNull(ExcelWorksheet worksheet, int row, int column, string columnName, StringBuilder exceptionMessage)
    {
        worksheet.Cells[row, column].Style.Numberformat.Format = "hh:mm:ss";
        var cellValue = TimeSpan.Parse(worksheet.Cells[row, column].Value.ToString());

        if (cellValue.ToString() != null && !string.IsNullOrWhiteSpace(cellValue.ToString()))
        {
            return cellValue;
        }

        exceptionMessage.Append(GetLocalizedExceptionMessagePart(columnName));
        return null;
    }

"worksheet.Cells[行, 列].Value" 来0.414953703703704

还有

“worksheet.Cells[row, column].Text” 09:12:32 出现

我怎样才能得到准确的值?

【问题讨论】:

    标签: c# excel asp.net-core import epplus


    【解决方案1】:

    Excel 以十进制值存储日期和时间。整数部分为日期,小数部分为时间。

    因此,要进入 C# DateTime,请使用 OLE 自动化转换器:

    try
    {
        var val = (double)worksheet.Cells[row, column].Value;
        var dt = DateTime.FromOADate(val);
        var cellValue = dt.TimeOfDay;
    }
    catch (Exception)
    {
        //log conversion error
    }
    

    想要在演员 JIC 周围放置一个Try.Catch,因为单元格中没有数字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 2014-07-18
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多