By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,637 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,637 IT Pros & Developers. It's quick & easy.

Reading date value from Excel

P: n/a
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.xlWindow s, "\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.ge t_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.Exce l.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.

May 4 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.


On May 4, 3:49 pm, Bharathi <bharathidot...@gmail.comwrote:
Hi,

I got strucked with readingdatevalue 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 thedateentered in
excel file.

Excel cell containsdatevalue in the format Mon-YYYY.

I should be able to extract thisdateas 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.xlWindow s, "\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.ge t_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.Exce l.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.

May 4 '07 #2

P: n/a
Hi,
Got the solution after I have searched for a while.
Esier yet, you can use DateTime.FromOADate and DateTime.ToOADate to
convert between .NET datetimes and Excel datetimes.

http://msdn2.microsoft.com/en-us/lib...romoadate.aspx

Hope it helps.
Best regards,
Adrian.
May 4 '07 #3

P: n/a
Thank you very much Adrian. Its so nice & so simple.

On May 4, 5:49 pm, "Adrian Gallero" <adrian@[nospam]tmssoftware.com>
wrote:
Hi,
Got the solution after I have searched for a while.

Esier yet, you can use DateTime.FromOADate and DateTime.ToOADate to
convert between .NET datetimes and Excel datetimes.

http://msdn2.microsoft.com/en-us/lib...romoadate.aspx

Hope it helps.
Best regards,
Adrian.

May 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.