A few weeks ago I made an article evolving around the intricate details of how to handle dates in SpreadsheetML. This is a follow-up on to that article.
SpreadsheetML (and OpenXml as such) is divided in two chunks - "the Transitional chunk" and "the Strict chunk". In short - the transitional chunk contains functionality aimed at enabling compatibility with documents created by the Office applications by made by Microsoft prior to 2007 - and compatibility with the applications themselves in addition to the other "legacy" productivity suites out there like iWork, OpenOffice.org, Lotus Symphony and others. "The Strict chunk" is stripped for all that legacy functionality and is more aimed at "the future", so to speak.
In my previous post I outlined how dates in cells in spreadsheets are really just styled numbers. But in Strict files this is not the case anymore. Dates are here simply what we know as "dates".
In short - when you want to use a date in a spreadsheet cell, you simply put in a standard ISO Date.
ISO-dates is a comprehensive thing - defined in a standard spanning some 40 pages in total. ISO dates cover literally everything regarding dates throughout all time in the past and in the future - including time zones etc. The support for ISO-dates in Strict SpreadsheetML is a limited subset of this functionality.
The support of ISO dates is defined in the standard of OpenXml in Part 1, Section 18.17.4 Dates and times. In short the limitations are:
The format for persisting dates in a Cell is this
At the end of the day we'll want to use this in our code using e.g. the OpenXml SDK.
There is one note however regarding the usage of the ISO dates, that you need to consider. Microsoft Excel has had read capability of Strict spreadsheets since Microsoft Office 2010. But previous versions of Microsoft Office does not support this and applications like LibreOffice or OpenOffice.org does not support this either ... yet.
Remember that the markup for a date in a Cell used to be like this
<row r="1"> <c r="A1" s="1"> <v>40957</v> </c> </row>
But in strict files the markup is now this
<row r="1"> <c r="A1" t="d" s="1"> <v>2012-03-01T14:30:00</v> </c> </row>
So two things have changed here:
So what has essentially changed is only the persisting format and the type of the date. Note also that everything with styling of the ISO date is as it has always been, so you'll still be able to style your ISO date above to be displayed as
... all according to your style settings.
When creating a date cell using the OpenXml SDK you'll need to add a new property on the cell, the DataType-property.
The code is this
//Create a new cell var c = new Cell { CellReference = "A1" }; // Create a Date CellValue var typeValue = new EnumValue<CellValues> { Value = CellValues.Date }; // Set the Cell value type to the cell c.DataType = typeValue; //Add an ISOO-date to the cell var value = new CellValue { Text = DateTime.Now.ToString("yyyy-MM-dd") }; // Append the value to the cell c.AppendChild(value);
And that's is really all there is to it.
Happy coding!