How to insert a date to an Open XML worksheet?

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

    When creating new SpreadsheetDocument from scratch, for Date formatting to work, minimal Stylesheet has to be created.

    Critical are those few lines:

    new CellFormat
    {
        NumberFormatId = 14,
        ApplyNumberFormat = true
    })
    

    Full Stylesheet class:

    using (var spreadSheet = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.Workbook))
    {
        // Workbook
        var workbookPart = spreadSheet.AddWorkbookPart();
        workbookPart.Workbook =
            new Workbook(new Sheets(new Sheet { Name = "Sheet1", SheetId = (UInt32Value) 1U, Id = "rId1" }));
    
        // Add minimal Stylesheet
        var stylesPart = spreadSheet.WorkbookPart.AddNewPart<WorkbookStylesPart>();
        stylesPart.Stylesheet = new Stylesheet
        {
            Fonts = new Fonts(new Font()),
            Fills = new Fills(new Fill()),
            Borders = new Borders(new Border()),
            CellStyleFormats = new CellStyleFormats(new CellFormat()),
            CellFormats =
                new CellFormats(
                    new CellFormat(),
                    new CellFormat
                    {
                        NumberFormatId = 14,
                        ApplyNumberFormat = true
                    })
        };
    
        // Continue creating `WorksheetPart`...
    

    After Stylesheet is added, DateTime can be formatted:

    if (valueType == typeof(DateTime))
    {
        DateTime date = (DateTime)value;
        cell.CellValue = new CellValue(date.ToOADate().ToString(CultureInfo.InvariantCulture));
    
        // "StyleIndex" is "1", because "NumberFormatId=14"
        // is in the 2nd item of `CellFormats` array.
        cell.StyleIndex = 1; 
    }
    

    Note that StyleIndex value depends on the order of CellFormat items in the CellFormats array or the Stylesheet object. In this example NumberFormatId = 14 item on the 2nd item in the array.

    0 讨论(0)
  • 2021-02-12 11:26

    There are 2 ways to store dates in OpenXml; by writing a number (using ToOADate) and setting the DataType to Number or by writing an ISO 8601 formatted date and setting the DataType to Date. Note that the default DataType is Number so if you go with the first option you don't have to set the DataType.

    Whichever method you choose, you'll need to set the style as Excel displays both methods identically. The following code shows an example of writing a date using the Number format (with and without explicitly setting the DataType) and using the ISO 8601 format.

    using (SpreadsheetDocument document = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
    {
        //fluff to generate the workbook etc
        WorkbookPart workbookPart = document.AddWorkbookPart();
        workbookPart.Workbook = new Workbook();
    
        var worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
        worksheetPart.Worksheet = new Worksheet();
    
        Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
    
        Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet" };
        sheets.Append(sheet);
    
        workbookPart.Workbook.Save();
    
        var sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
    
        //add the style
        Stylesheet styleSheet = new Stylesheet();
    
        CellFormat cf = new CellFormat();
        cf.NumberFormatId = 14;
        cf.ApplyNumberFormat = true;
    
        CellFormats cfs = new CellFormats();
        cfs.Append(cf);
        styleSheet.CellFormats = cfs;
    
        styleSheet.Borders = new Borders();
        styleSheet.Borders.Append(new Border());
        styleSheet.Fills = new Fills();
        styleSheet.Fills.Append(new Fill());
        styleSheet.Fonts = new Fonts();
        styleSheet.Fonts.Append(new Font());
    
        workbookPart.AddNewPart<WorkbookStylesPart>();
        workbookPart.WorkbookStylesPart.Stylesheet = styleSheet;
    
        CellStyles css = new CellStyles();
        CellStyle cs = new CellStyle();
        cs.FormatId = 0;
        cs.BuiltinId = 0;
        css.Append(cs);
        css.Count = UInt32Value.FromUInt32((uint)css.ChildElements.Count);
        styleSheet.Append(css);
    
        Row row = new Row();
    
        DateTime date = new DateTime(2017, 6, 24);
    
        /*** Date code here ***/
        //write an OADate with type of Number
        Cell cell1 = new Cell();
        cell1.CellReference = "A1";
        cell1.CellValue = new CellValue(date.ToOADate().ToString());
        cell1.DataType = new EnumValue<CellValues>(CellValues.Number);
        cell1.StyleIndex = 0;
        row.Append(cell1);
    
        //write an OADate with no type (defaults to Number)
        Cell cell2 = new Cell();
        cell2.CellReference = "B1";
        cell2.CellValue = new CellValue(date.ToOADate().ToString());
        cell1.StyleIndex = 0;
        row.Append(cell2);
    
        //write an ISO 8601 date with type of Date
        Cell cell3 = new Cell();
        cell3.CellReference = "C1";
        cell3.CellValue = new CellValue(date.ToString("yyyy-MM-dd"));
        cell3.DataType = new EnumValue<CellValues>(CellValues.Date);
        cell1.StyleIndex = 0;
        row.Append(cell3);
    
        sheetData.AppendChild(row);
    
        worksheetPart.Worksheet.Save();
    }
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-02-12 11:31

    a) Get compatibility with Excel 2007, Excel 2007 Viewer etc. b) DateTime before 1.1.1900 write as string.

    DateTime dat = (DateTime)dr[dc.ColumnName];
    
    //Not working with Excel 2007
    //cell.DataType = CellValues.Date;
    //cell.CellValue = new CellValue(dat.ToString("s"));
    
    double diff = (dat - new DateTime(1899, 12, 30)).TotalSeconds / 86400.0;
    if (diff > 1)
    {
        cell.DataType = CellValues.Number;
        cell.CellValue = new CellValue(diff.ToString().Replace(",", "."));
    
        if (dat.TimeOfDay == new TimeSpan(0))
        {                                
            cell.StyleIndex = 2;   //Custom Style NumberFormatId = 14 ( d/m/yyyy)
        }
        else
        {
            cell.StyleIndex = 1;   //Custom Style NumberFormatId = 22 (m/d/yyyy H:mm)
        }
    }
    else
    {
        cell.DataType = CellValues.String;
        cell.CellValue = new CellValue(dat.ToString());
    }
    
    0 讨论(0)
  • 2021-02-12 11:35

    You have to convert DateTime to double using function ToOADate i.e.:

    DateTime dtValue = DateTime.Now;
    string strValue = dtValue.ToOADate().ToString(CultureInfo.InvariantCulture);
    

    then set it as CellValue

    Cell cell;
    cell.DataType = new EnumValue<CellValues>(CellValues.Date);
    cell.CellValue = new CellValue(strValue);
    

    Remember to format cell using DateTime formatting, otherwise you will see double value, not date.

    0 讨论(0)
  • 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.

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