c# - 从 Excel 工作表中读取日期时间值

标签 c# excel office-interop

当我试图从 Excel 工作表中读取日期时间类型的值时,它返回一个 double 值。例如,如果想要读取值 '2007-02-19 14:11:45.730' 像这样,我得到一个 double 类型的值。此外,我正在使用时间跨度转换这个 double 值,但没有成功完成,因为我只得到这个值 '2007-02-19 12:00:00 AM'< br/> 现在我想要与第一个完全相同的日期时间值。我的代码是这样的:-

TimeSpan datefromexcel = new TimeSpan(Convert.ToInt32((range.Cells[rCnt, cCnt] as Excel.Range).Value2), 0, 0, 0);

  DateTime inputdate = new DateTime(1900, 1, 1).Add(datefromexcel);

   arrrow2[cCnt - 1] = inputdate.ToString();

求助!!! 谢谢。

最佳答案

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

double d = double.Parse(b);
DateTime conv = DateTime.FromOADate(d);

关于c# - 从 Excel 工作表中读取日期时间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4538321/

相关文章:

c# - QBXML:简单的 CustomerAdd "error when parsing"

c# - ASP.NET 5 (vNext) 网络项目 : library conflict upgrading from beta4 to beta6

c# - 使用c#删除书签的内容而不删除ms word中的书签

java - 双引号中的值在导出到 Excel 时不起作用

excel - 将数据从 DBGrid 导出到 Excel

c# - 使用互操作创建 excel 文件时防止打开 Excel

c# - HTMLBody 拒绝输出我指定的字体大小,总是以不同的大小结束

c# - 如何在运行时通过 retrofit 设置用户代理?

c# - Xamarin - 如何淬火或过滤应用程序输出?

vba - CheckSpelling 函数中的类型不匹配