NPOI DateCell 与 Excel 中的时间相差 1 秒

NPOI DateCell is 1 second off from time in Excel

我有一个 excel 文档,时间列为“02:30”……但是,当使用 NPOI 遍历它们时,DateCellValue 翻转为“02:29:59”……这首先发生在 2019 年 1 月 1 日(正确存储为“02:30”),但随后在 2019 年 1 月 2 日翻转为“02:29:59”……有谁知道如何让它简单地得到不尝试对它做任何巫术的情况下,细胞的价值?很明显,它可能考虑到了闰秒之类的东西?然而,它在 Excel 中作为“02:30” 清楚地显示在我的断点处:

[Model].DepartureDttm = row.GetCell(j).DateCellValue [1/2/2019 2:29:59 AM]

您不是唯一遇到此问题的人。这是一个很好的 answer.

您可以使用 NPOI 附带的 DateUtil.GetJavaDate 来解决此问题。您可以创建此基本扩展方法:

public static class Extensions
{
    public static DateTime DateCellValueRounded(this ICell cell)
    {
        return DateUtil.GetJavaDate(cell.NumericCellValue, false, TimeZone.CurrentTimeZone, true);
    }
}

然后像这样使用它:

DateTime date = row.GetCell(index).DateCellValueRounded;

GetJavaDate 签名(设置为 true 的最后一个参数完成工作):

public static DateTime GetJavaDate(double date, bool use1904windowing, TimeZone tz, bool roundSeconds);