Click here to Skip to main content
15,917,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
BillWoodruff 17-Nov-15 9:29am    
What exactly are you doing in C# here: are you reading in data (time values) already in the Excel WorkSheet, or are you computing those values in C# and writing them into the Excel WorkSheet ?

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)

C#
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.
C#
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.
 
Share this answer
 
Comments
BillWoodruff 17-Nov-15 5:08am    
+5 would be interesting to know if OP reads the cells in a as a "range" (array), in which case Linq could be used for the sum.
George Jonsson 17-Nov-15 9:06am    
That is of course an option.
However, my LINQ knowledge is kind of crappy. so I usually don't think in those terms.
BillWoodruff 17-Nov-15 9:28am    
It may be possible that the OP is fully controlling Excel through C#, in which case your solutions is perfect; all the OP has to do is write the computed results into the Cells.
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.
 
Share this answer
 
Comments
George Jonsson 17-Nov-15 9:11am    
Time is difficult and always going to fast.
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
 
Share this answer
 
Comments
George Jonsson 16-Nov-15 22:42pm    
The question was how to do it in C#.
(I think)
Patrice T 16-Nov-15 22:53pm    
It is possible, but in this case, there no need to speak about Excel at all.
More details would be welcome.
George Jonsson 16-Nov-15 22:55pm    
True. But it is better to confuse the enemy. :-)
Patrice T 16-Nov-15 23:02pm    
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900