如何使用c#在excel中汇总时间列 [英] How to sum time column in excel using c#
问题描述
大家好,
我能否知道如何使用c#在excel中对时间列值进行求和。我正在使用OLEDB从excel通过c#代码读取数据。
以下是我的时间列值。
TimeColumn
==========
0:02:37
0:03:58
0:02 :31
=========
总和:0:09:06
问候
Hyma
在C#中,如果将值转换为TimeSpan [ ^ ]类型。
(我在这里假设这里的时间表示持续时间而不是一天中的时间。
TimeSpan ts1 = TimeSpan.Parse( 0:02:37);
TimeSpan ts2 = TimeSpan.Parse( 0:03:58);
TimeSpan ts3 = TimeSpan.Parse( 0:02:31);
TimeSpan sum = new TimeSpan();
sum = sum.Add(ts1);
sum = sum.Add(ts2);
sum = sum.Add(ts3);
当然你可以跳过临时变量ts1-ts3。
sum = sum.Add(TimeSpan.Parse( 0:02:37));
但最好从单独的变量开始,以便更容易调试。
当然你应该在一个循环中这样做,但这取决于你。
是的,你可以使用标准的加法公式来添加具有'时间值的单元格。 />
但是,如果您遇到总时间超过24小时的情况,则需要执行以下操作:[ ^ ]。
在任何情况下,最好选择要显示时间总计的单元格并选择自定义格式,如:[h]:mm显示浩urs和分钟,或[h]:mm:ss显示小时,分钟,秒。
在Excel中,3个单元格的简单总和为您提供答案。
Excel自动识别时间值。
所以,如果你有
A1 = 0:02:37
A2 = 0 :03:58
A3 = 0:02:31
a简单总和给你的结果
A4 = A1 + A2 + A3
Hi all,
Can i know how to sum time column values in excel using c#. I am using OLEDB for reading data from excel through c# code.
Below is my time column value.
TimeColumn
==========
0:02:37
0:03:58
0:02:31
=========
sum: 0:09:06
Regards
Hyma
In C# it is easier to do addition if you convert your values into the TimeSpan[^] type.
(I assume here that the times here represents a duration and not the time of the day)
TimeSpan ts1 = TimeSpan.Parse("0:02:37"); TimeSpan ts2 = TimeSpan.Parse("0:03:58"); TimeSpan ts3 = TimeSpan.Parse("0:02:31"); TimeSpan sum = new TimeSpan(); sum = sum.Add(ts1); sum = sum.Add(ts2); sum = sum.Add(ts3);
Of course you can skip the temporary variable ts1-ts3.
sum = sum.Add(TimeSpan.Parse("0:02:37"));
but it might be better to start with separate variables to make it easier to debug.
Then of course you should probably do this in a loop, but that is up to you.
Yes, you can use standard addition formulas to add cells with 'time values.
However, if you ever have a case where the total time is greater than 24 hours, you need to do something like this: [^].
In any case it's good practice to select the cells you want to show time totals in and choose a custom format like: [h]:mm to show hours and minutes, or [h]:mm:ss to show hours, minutes, seconds.
In Excel, the simple sum of the 3 cells gives you the answer.
Excel automatically recognize a time value.
So, if you have
A1=0:02:37
A2=0:03:58
A3=0:02:31
a simple sum gives you the result
A4=A1+A2+A3
这篇关于如何使用c#在excel中汇总时间列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!