How to insert a date to an Open XML worksheet?

后端 未结 7 1228
渐次进展
渐次进展 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:31

    Use Shared String:

    // assuming it's the first item in the shared string table
    SharedStringItem sharedStringItem = new SharedStringItem();
    Text text = new Text();
    text.Text = DateTime.Today.ToString("MM/dd/yyyy hh:mm");
    sharedStringTable1.Append(sharedStringItem);
    

    Then later in code:

    // assuming it's the first item in the shared string table
    var cell = new Cell {CellReference = "A1", DataType = CellValues.SharedString};
    var cellValue = new CellValue("0");
    cell.Append(cellValue);
    

提交回复
热议问题