OpenXML - Writing a date into Excel spreadsheet results in unreadable content

前端 未结 7 819
离开以前
离开以前 2020-12-10 11:21

I am using the following code to add a DateTime to a column in my spreadsheet:

var dt = DateTime.Now;
r.AppendChild(new Cell()
    {         


        
7条回答
  •  隐瞒了意图╮
    2020-12-10 12:15

    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.

提交回复
热议问题