从 Excel 工作表中读取日期时间值 [英] Reading Datetime value From Excel sheet

查看:46
本文介绍了从 Excel 工作表中读取日期时间值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

when am trying to read datetime type value from excel sheet it is returning a double value.for example if want to read value '2007-02-19 14:11:45.730' like this, i am getting a double type value .further i am converting this double value using timespan,but not complete successfully because i am getting only this value '2007-02-19 12:00:00 AM'
now i want exact same datetime value as first one. My code is like :-

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 自动化转换为 .net 格式.

You need to convert the date format from OLE Automation to the .net format by using DateTime.FromOADate.

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

这篇关于从 Excel 工作表中读取日期时间值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆