Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from a Excel Cell

Tags:

excel

epplus

I am using EPPlus package to read from an .xlsx file. I need to read a TimeSpan object from a cell. The excel cell would contain a data in "hh:mm" format, for example: "09:00" or "18:30" without the quotation marks.

Here is my code:

string myString = workSheet.Cells[1,1].Value.ToString();
double d = double.Parse(myString);
DateTime dt = DateTime.FromOADate(d);
TimeSpan ts = new TimeSpan(dt.Hour, dt.Minute, dt.Second);

for "09:00" or "08:30" data in excel file myString contains "0.375", or "0.354166666666667" respectively and ts can be calculated as expected. But for "10:00" in excel file myString contains "30/12/1899 10:00:00 AM" and the parsing of the double value fails hence ts cannot be calculated.

I am not sure why this is happening inconsistently . How can I consistently read a double value from an excel cell (more precisely a cell with a General format)? Please help.

Here is my simplified code (After the suggestion from Siddharth Rout):

string myString = workSheet.Cells[1,1].Text.ToString();
TimeSpan ts = TimeSpan.Parse(myString);

So far it is working with various values.

like image 429
Choudhury Avatar asked Sep 24 '13 13:09

Choudhury


1 Answers

Here is my simplified code (After the suggestion from Siddharth Rout):

string myString = String.Format("{0}", workSheet.Cells[1,1].Text);
TimeSpan ts = TimeSpan.Parse(myString);

So far it is working with various values.

like image 75
Choudhury Avatar answered Oct 14 '22 02:10

Choudhury