0

我写到 Excel 的日期时间总是四舍五入到第二天:

workSheet.Cells[0, 0] = new Cell(DateTime.Now, new CellFormat(CellFormatType.DateTime, @"HH:mm:ss"));

在输出 Excel 文件中,单元格获取此值:29/09/2013 00:00:00

此示例中的 DateTime.Now 是 28/09/2013 19:42:23

4

3 回答 3

0

如果您使用 ExcelLibrary 项目源代码,您可以通过以下方式解决此问题:

  1. 转到此位置的 SharedResource 类:[项目源代码文件夹]\Office\BinaryFileFormat 文件夹

  2. 更改 EncodeDateTime 函数如下:

    public double EncodeDateTime(DateTime value)
    {
        double days = (value - BaseDate).Days;
        //if (days > 365) days++;
        return days;
    }
    
  3. 使用首选格式将 DataTime 对象传递给 Cell:

    worksheet.Cells[iIndex, j] = new Cell(((DateTime)cellValue), new CellFormat(CellFormatType.DateTime, @"dd/MM/yyyy"));
    
于 2014-02-19T02:46:52.287 回答
0

我最终将单元格值作为字符串而不是 DateTime 传递:

 workSheet.Cells[0, 0] = new Cell(DateTime.Now.ToString(@"HH:mm:ss:ff"),
                                  new CellFormat(CellFormatType.DateTime, @"HH:mm:ss"));
于 2013-09-29T10:41:15.260 回答
0

您需要使用 DateTime.FromOADate 将日期格式从 OLE 自动化转换为 .net 格式。

If oCell.Format.FormatType = CellFormatType.Date OrElse oCell.Format.FormatType = CellFormatType.DateTime Then
 Dim d As Double = Double.Parse(oCell.Value)

 Debug.print(DateTime.FromOADate(d))

End If
于 2016-09-14T06:17:02.827 回答