简体   繁体   English

像excel一样将excel格式转换为C#时间戳

[英]Convert excel format to C# TimeStamp like excel do so

In my app I'm reading some data from excel sheet and i stucked in the moment when i want to get timestamp from a cell. 在我的应用程序中,我从Excel工作表中读取一些数据,当我想从单元格获取时间戳时,我陷入了困境。 In this speciffic cell i got a value '1900-01-02 13:20:04' and in excel i am using format '[h]:mm:ss' which formats this value to '61:20:04', which is correct beacuse those cells are showing hours worked by some employees. 在这个特殊的单元格中,我得到的值为'1900-01-02 13:20:04',而在excel中,我使用的格式为[[h]:mm:ss',该值的格式为'61:20:04',即是正确的,因为这些单元格显示出某些员工的工作时间。 What I want to achieve is to read this data from excel in the same format as it's formated in excel('61:20:04') and save it to string. 我要实现的是以与excel('61:20:04')相同的格式从excel读取此数据,并将其保存到字符串中。 My problem is how to read this, or convert to this timestamp value ? 我的问题是如何读取此内容或将其转换为此时间戳值?

EDIT.(the way i am doing it now) 编辑。(我现在的方式)

I am reading the code from excel using ODBC so just select data i need(like sQl select) -> add all data to datatable and then i'm just doing 我正在使用ODBC从excel中读取代码,因此只需选择我需要的数据(如sQl select)->将所有数据添加到数据表中,然后我就在做

dr["workedHours"].ToString() -(dr = DataRow in my DataTable.Rows) this line returns '1900-01-02 13:20:04'. dr [“ workedHours”]。ToString()-(dr =我的DataTable.Rows中的DataRow)此行返回“ 1900-01-02 13:20:04”。 How can I convert it to time like excel does. 我如何像excel一样将其转换为时间。

Some proof of concept: 一些概念证明:

var timeStamp = "1900-01-02 13:20:04";
var date = DateTime.Parse(timeStamp);
var stamp = date.Subtract(new DateTime(1900, 1, 1));
var result = string.Format("{0}:{1:00}:{2:00}", 24 + stamp.Days * 24 + stamp.Hours, stamp.Minutes, stamp.Seconds);

It seems for some reason Excel takes time stamps from 0 January 1900, this is why I add 24 in hours result. 出于某种原因,Excel从1900年1月0日开始使用时间戳记,这就是为什么我将24小时的结果相加。

I assumed you always want format hh:mm:ss, even for days, month, etc. 我以为你总是想要格式hh:mm:ss,即使是几天,一个月等等。

Without any code provided Ill go away from 'how to read data the right way' and point to 'how to convert your output'. 如果没有提供任何代码,Ill将放弃“如何以正确的方式读取数据”,而转向“如何转换输出”。

Could work like this: 可以这样工作:

string[] stampParts = output.ToString().Split(' ');
string[] dateParts = stampParts[0].Split('-');
string[] timeParts = stampParts[1].Split(':');

var result = new TimeSpan(((Convert.ToInt32(dateParts[2])-1)*24) + Convert.ToInt32(timeParts[0]), Convert.ToInt32(timeParts[1]), Convert.ToInt32(timeParts[2]));

This would add the days from the date with 24 hours by a single one to the hours of your time. 这会将从24小时的日期算起的天数加1。
Notice: I left out calculating the months here. 注意:我没有在这里计算月份。 So this code is limited in calculation up one month. 因此,此代码最多只能计算一个月。 If you want to output larger numbers (over about 720 hours) the months have to be included into the calculation. 如果要输出更大的数字(超过720小时),则必须将月份包括在计算中。

Edit after code was provided: 提供代码后进行编辑:
Sorry, I havnt worked with odbc-connections before. 抱歉,我以前曾使用过odbc-connections。 But the point is: You are getting the raw-value instead of the calculated one. 但关键是:您将获得原始值,而不是计算出的原始值。 I already tried googling for that, but couldnt find much results. 我已经尝试过使用Google搜索,但是找不到很多结果。

Maybe someone else can help out with this. 也许其他人可以帮忙。 Instead I would suggest just converting the output. 相反,我建议只转换输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM