How to insert a date to an Open XML worksheet?

后端 未结 7 1238
渐次进展
渐次进展 2021-02-12 10:42

I\'m using Microsoft Open XML SDK 2 and I\'m having a really hard time inserting a date into a cell. I can insert numbers without a problem by setting Cell.DataType = Cell

7条回答
  •  暖寄归人
    2021-02-12 11:39

    The following worked for us:

    c.CellValue = new CellValue(datetimeValue).ToOADate().ToString());
    c.DataType = CellValues.Number;
    c.StyleIndex = StyleDate;
    

    Set the DataType to CellValues.Number and then be sure to format the cell with the appropriate style index from the CellFormats. In our case we build a stylesheet within the worksheet, and StyleDate is an index into the CellFormats in the stylesheet.

提交回复
热议问题