Reading date value from Excel

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bharathi

    #1

    Reading date value from Excel

    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.Offic e.Interop.Excel .Workbook theWorkbook
    = obj.Workbooks.O pen(file1.FileN ame, 0, true, 5, "", "", false,
    Microsoft.Offic e.Interop.Excel .XlPlatform.xlW indows, "\t", false,
    false, 0, true, false, false);

    Microsoft.Offic e.Interop.Excel .Sheets sheet =
    theWorkbook.Wor ksheets;

    Microsoft.Offic e.Interop.Excel .Worksheet worksheet =
    (Microsoft.Offi ce.Interop.Exce l.Worksheet)she et.get_Item(1);

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

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


    When I use Datetime conversion like this
    Convert.ToDateT ime(((Microsoft .Office.Interop .Excel.Range)wo rksheet.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.

  • Bharathi

    #2
    Re: Reading date value from Excel

    Got the solution after I have searched for a while.

    & the solution goes here:


    TimeSpan datefromexcel = new
    TimeSpan(Conver t.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(datefrom excel);

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




    On May 4, 3:49 pm, Bharathi <bharathidot... @gmail.comwrote :
    Hi,
    >
    I got strucked with readingdatevalu e 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 containsdateval ue in the format Mon-YYYY.
    >
    I should be able to extract thisdateas it is.
    >
    Please help me...........
    >
    Please go through the code snippet:
    Microsoft.Offic e.Interop.Excel .Workbook theWorkbook
    = obj.Workbooks.O pen(file1.FileN ame, 0, true, 5, "", "", false,
    Microsoft.Offic e.Interop.Excel .XlPlatform.xlW indows, "\t", false,
    false, 0, true, false, false);
    >
    Microsoft.Offic e.Interop.Excel .Sheets sheet =
    theWorkbook.Wor ksheets;
    >
    Microsoft.Offic e.Interop.Excel .Worksheet worksheet =
    (Microsoft.Offi ce.Interop.Exce l.Worksheet)she et.get_Item(1);
    >
    Microsoft.Offic e.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.ToDateT ime(((Microsoft .Office.Interop .Excel.Range)wo rksheet.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.

    Comment

    • Adrian Gallero

      #3
      Re: Reading date value from Excel

      Hi,
      Got the solution after I have searched for a while.
      Esier yet, you can use DateTime.FromOA Date and DateTime.ToOADa te to
      convert between .NET datetimes and Excel datetimes.

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

      Hope it helps.
      Best regards,
      Adrian.

      Comment

      • Bharathi

        #4
        Re: Reading date value from Excel

        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.FromOA Date and DateTime.ToOADa te to
        convert between .NET datetimes and Excel datetimes.
        >
        http://msdn2.microsoft.com/en-us/lib...romoadate.aspx
        >
        Hope it helps.
        Best regards,
        Adrian.

        Comment

        Working...