Excel Interop cell formatting of Dates

后端 未结 2 714
失恋的感觉
失恋的感觉 2020-12-19 12:57

My program creates an output in Excel.

Some of the dates seem to be getting misinterpreted: \"enter

相关标签:
2条回答
  • 2020-12-19 13:48

    Just replace (to string) with (formated value)

    0 讨论(0)
  • 2020-12-19 13:58

    The issue here is that you are writing everything to Excel as a string, and Excel will store it as such. On editing and pressing Enter, Excel will re-interpret the cell contents and may then store it differently.

    When you call ToString() on a DateTime, the default formatting is in the form DD/MM/YY HH:mm:ss, which is exactly what you are seeing in the resultant file.

    Dates in Excel are stored as numbers representing the number of days that have elapsed since 1900. To get this number, you can call the ToOADate() method.

    See http://msdn.microsoft.com/en-gb/library/system.datetime.tooadate.aspx

    With the data set as a doubleand an appropriate format string, you should get the result you're hoping for!

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