【问题标题】:Excel cell format [$-10409]m-d-yyyy h:mm:ss AM/PM [duplicate]Excel 单元格格式 [$-10409]m-d-yyyy h:mm:ss AM/PM [重复]
【发布时间】:2016-07-25 13:46:49
【问题描述】:

我有一个从 MS excel 文件中读取数据的 c# 应用程序。 excel单元格格式为[$-10409]m-d-yyyy h:mm:ss AM/PM

我正在使用 NuGet 中的 ExcelDataReader 从 excel 文件中读取数据。

var file = new FileInfo(strFilePath);
        using (var stream = new FileStream(strFilePath, FileMode.Open))
        {
            IExcelDataReader reader = null;
            if (file.Extension == ".xls")
            {
                reader = ExcelReaderFactory.CreateBinaryReader(stream);

            }
            else if (file.Extension == ".xlsx")
            {
                reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
            }

            ds = reader.AsDataSet();
            dt = ds.Tables[0];
        }

当我读取数据时,值会发生变化。例如7-1-2016 11:05:00 AM 转换为 42552.4618055556

有什么方法可以在阅读时得到正确的值吗?

【问题讨论】:

  • 谢谢@Scroog1。它工作:)

标签: c# excel exceldatareader


【解决方案1】:

您看到的双精度日期是自 1900-01-01 以来的天数。如果您采取42552.46/365=116.58

不建议使用此方法翻译,因为闰年​​确实存在。您可以通过以下方式重新格式化单元格:

  1. 突出显示单元格
  2. 点击格式化单元格
  3. 选择日期
  4. 选择日期可视化格式

【讨论】:

  • Excel 文件(我的应用程序的输入)是从其他应用程序 (SSIS) 生成的。我无法重新格式化它。
猜你喜欢
  • 1970-01-01
  • 2012-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 2020-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多