I’m using C# to write out an OpenDocument spreadsheet in XML. No problem with writing double values, which work fine just like this:
private void SaveFloatCell(XmlNode rowNode, XmlDocument ownerDocument, double number)
{
XmlElement cellNode = ownerDocument.CreateElement("table:table-cell", this.GetNamespaceUri("table"));
XmlAttribute valueType = ownerDocument.CreateAttribute("office:value-type", this.GetNamespaceUri("office"));
valueType.Value = "float";
cellNode.Attributes.Append(valueType);
XmlAttribute value = ownerDocument.CreateAttribute("office:value", this.GetNamespaceUri("office"));
value.Value = number.ToString(CultureInfo.InvariantCulture);
cellNode.Attributes.Append(value);
rowNode.AppendChild(cellNode);
}
However, when trying to save DateTime values I can’t get them to display properly. This is what I have so far, which displays “2012” in the cell instead of the specified Date format:
private void SaveDateTimeCell(XmlNode rowNode, XmlDocument ownerDocument, double number, string format)
{
XmlElement cellNode = ownerDocument.CreateElement("table:table-cell", this.GetNamespaceUri("table"));
XmlAttribute valueType = ownerDocument.CreateAttribute("office:value-type", this.GetNamespaceUri("office"));
valueType.Value = "date";
cellNode.Attributes.Append(valueType);
XmlAttribute value = ownerDocument.CreateAttribute("office:value", this.GetNamespaceUri("office"));
value.Value = Utils.DateTime(number).ToString("yyyy-mm-ddThh:mm:ss");
cellNode.Attributes.Append(value);
rowNode.AppendChild(cellNode);
}
I’ve spent quite a while playing with various code snippets I’ve found, but with no success. On the offchance, is there any generous soul out there that can help me with this?
Many thanks!
Ok, I have the answer. The final code looks like this:
It’s the definition of “ce2” which is key. This is done in the styles.xml file of the unzipped *.ods file:
This date-style N19 is then referred to in the automatic-styles of the content.xml of the unzipped *.ods file thus:
In fact, Jon’s suggestions were necessary as well for the code to work perfectly. So thanks again Jon. Anyhow, a black, black art indeed … 🙂