Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NPOI DateCell在Excel中的时间间隔为1秒_Excel_Date_Npoi - Fatal编程技术网

NPOI DateCell在Excel中的时间间隔为1秒

NPOI DateCell在Excel中的时间间隔为1秒,excel,date,npoi,Excel,Date,Npoi,我有一个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.Get

我有一个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]

你不是唯一一个面对这个问题的人。这是一个好主意

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

公共静态类扩展
{
公共静态DateTime DateCellValueRounded(此ICell单元格)
{
return DateUtil.GetJavaDate(cell.NumericCellValue,false,TimeZone.CurrentTimeZone,true);
}
}
然后像这样使用它:

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

GetJavaDate
signature(最后一个参数设置为
true
执行此任务):

publicstaticdatetime GetJavaDate(双日期,booluse1904窗口,时区tz,boolroundseconds);

我也尝试过使用新的DateTime(row.GetCell(j).DateCellValue.Ticks,DateTimeKind.Utc)得到相同的结果。。休息1秒钟,有些进步。我相信用户做了Excel拖动技巧,每行增加1天,这可能会把事情搞砸。我手动重写了2019年1月2日02:30,并更正了。。。但问题仍然存在,有什么方法可以简单地计算细胞的价值?将其更改为字符串不是一个选项-要求它必须是自定义格式为“d-mmm-yyyy h:mm”的日期单元格。。。真正的用户可能不会使用拖拽技巧,因为出发时间每天都在变化,但最好编写代码以防万一。你应该把这些评论放在你的问题中,特别是第二个评论,它是重现问题的关键。