Reading date value from Excel

B

Bharathi

Hi,

I got strucked with reading date value from excel file using
C#.NET.

For Jan-2000 the value I am getting is 36526.0.

For all other dates also I am getting some double value like this.

Is there any manipulation so that I can find out the date entered in
excel file.

Excel cell contains date value in the format Mon-YYYY.

I should be able to extract this date as it is.

Please help me...........

Please go through the code snippet:
Microsoft.Office.Interop.Excel.Workbook theWorkbook
= obj.Workbooks.Open(file1.FileName, 0, true, 5, "", "", false,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false,
false, 0, true, false, false);

Microsoft.Office.Interop.Excel.Sheets sheet =
theWorkbook.Worksheets;

Microsoft.Office.Interop.Excel.Worksheet worksheet =
(Microsoft.Office.Interop.Excel.Worksheet)sheet.get_Item(1);

Microsoft.Office.Interop.Excel.Range R;
object[,] val;

R = worksheet.get_Range("A1", "B1");
val = (object[,])R.Cells.Value2;


When I use Datetime conversion like this
Convert.ToDateTime(((Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1,
3]).Value2)
I am getting a run-time error saying can't convert from double to
datetime.


Please help me...

Thanks in advance,
Bharathi.
 
B

Bharathi

Got the solution after I have searched for a while.

& the solution goes here:


TimeSpan datefromexcel = new
TimeSpan(Convert.ToInt32(val[1, 1])-2, 0, 0, 0);
//
val[1,1] is the cell value that is extracted from excel.
DateTime inputdate = new DateTime(1900, 1,
1).Add(datefromexcel);

//Finally inputdate gives the date entered in excel
file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top