How to convert “double” to “datetime” between Excel and c#

前端 未结 4 1000
执笔经年
执笔经年 2021-01-23 22:10

I have a c# program which needs to create an excel object, and do some operations, here are parts of my code:

// c# code:

workSheet.Cells[1, 1] = \"=2012/9/20\         


        
相关标签:
4条回答
  • 2021-01-23 22:51

    If you want to store a literal or preformatted value in an Excel cell, precede the value with a single quote '. For example, workSheet.Cells[1, 1] = "'2012/9/20";.

    0 讨论(0)
  • 2021-01-23 23:02

    Dude, your excel value is not a date. Drop the equal sign at the beginning.

    Once you do that you will get a proper double value for your time (which can be converted via the OADate function). What your getting now is nonsense.

    0 讨论(0)
  • 2021-01-23 23:16

    You can try this:

    First set the format in the cell:

     ws.Cells[1,1].Style.Numberformat.Format = "yyyy/MM/dd";
    

    Then set value as DateTime:

    workSheet.Cells[1, 1] =new DateTime(2012,9,20);
    

    And to get value use the following:

    double d = double.Parse(workSheet.Cells[1, 1].value());
    DateTime conv = DateTime.FromOADate(d);
    
    0 讨论(0)
  • 2021-01-23 23:17

    By typing is `=2012/9/20" you're telling Excel that this is a formula. 2012 divided by 9, divided by 20 equal 11.17777777778.

    Try storing the date without the = sign.

    0 讨论(0)
提交回复
热议问题