2012-04-24 28 views
2

我使用C#以XML格式寫出OpenDocument電子表格。沒有問題,寫雙重價值,它做工精細,就像這樣:如何在OpenDocument中保存.NET DateTime

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); 
    } 

然而,試圖挽救DateTime值的時候,我不能讓他們正確顯示。這是我到目前爲止,它顯示「2012」的單元格,而不是指定的日期格式:

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); 
    } 

我花了相當長一段時間的各種代碼段,我發現玩,但沒有成功。關於這個問題,有沒有可以幫助我的慷慨靈魂?

非常感謝!

回答

0

好的,我有答案。最終代碼如下所示:

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:date-value", this.GetNamespaceUri("office")); 
     value.Value = Utils.DateTime(number).ToString("yyyy-MM-ddTHH:mm:ss"); 
     cellNode.Attributes.Append(value); 

     XmlAttribute tableStyleName = ownerDocument.CreateAttribute("table:style-name", this.GetNamespaceUri("table")); 
     tableStyleName.Value = "ce2"; 
     cellNode.Attributes.Append(tableStyleName); 

     rowNode.AppendChild(cellNode); 
    } 

這是「ce2」的定義,它是關鍵。這是在解壓縮*的.ods文件的styles.xml文件中完成:

private void SaveStyleSheet(XmlNode sheetsRootNode) 
    { 
     XmlDocument ownerDocument = sheetsRootNode.OwnerDocument; 

     XmlNode sheetNode = ownerDocument.CreateElement("number:date-style", this.GetNamespaceUri("number")); 

     XmlAttribute styleName = ownerDocument.CreateAttribute("style:name", this.GetNamespaceUri("style")); 
     styleName.Value = "N19"; 
     sheetNode.Attributes.Append(styleName); 

     XmlElement numberDay = ownerDocument.CreateElement("number:day", this.GetNamespaceUri("number")); 
     XmlAttribute numberStyle = ownerDocument.CreateAttribute("number:style", this.GetNamespaceUri("number")); 
     numberStyle.Value = "long"; 
     numberDay.Attributes.Append(numberStyle); 
     sheetNode.AppendChild(numberDay); 

     XmlElement numberText = ownerDocument.CreateElement("number:text", this.GetNamespaceUri("number")); 
     numberText.InnerText = "/"; 
     sheetNode.AppendChild(numberText); 

     XmlElement numberMonth = ownerDocument.CreateElement("number:month", this.GetNamespaceUri("number")); 
     XmlAttribute numberStyle2 = ownerDocument.CreateAttribute("number:style", this.GetNamespaceUri("number")); 
     numberStyle2.Value = "long"; 
     numberMonth.Attributes.Append(numberStyle2); 
     sheetNode.AppendChild(numberMonth); 

     XmlElement numberText2 = ownerDocument.CreateElement("number:text", this.GetNamespaceUri("number")); 
     numberText2.InnerText = "/"; 
     sheetNode.AppendChild(numberText2); 

     XmlElement numberYear = ownerDocument.CreateElement("number:year", this.GetNamespaceUri("number")); 
     XmlAttribute numberStyle3 = ownerDocument.CreateAttribute("number:style", this.GetNamespaceUri("number")); 
     numberStyle3.Value = "long"; 
     numberYear.Attributes.Append(numberStyle3); 
     sheetNode.AppendChild(numberYear); 

     sheetsRootNode.AppendChild(sheetNode); 
    } 

此日期風格的N19然後在自動方式解壓後的*的.ods文件的content.xml的簡稱因此:

private void SaveAutomaticStyleSheet(XmlNode sheetsRootNode) 
    { 
     XmlDocument ownerDocument = sheetsRootNode.OwnerDocument; 

     XmlNode sheetNode = ownerDocument.CreateElement("style:style", this.GetNamespaceUri("style")); 

     XmlAttribute styleName = ownerDocument.CreateAttribute("style:name", this.GetNamespaceUri("style")); 
     styleName.Value = "ce2"; 
     sheetNode.Attributes.Append(styleName); 

     XmlAttribute styleFamily = ownerDocument.CreateAttribute("style:family", this.GetNamespaceUri("style")); 
     styleFamily.Value = "table-cell"; 
     sheetNode.Attributes.Append(styleFamily); 

     XmlAttribute styleParentStyleName = ownerDocument.CreateAttribute("style:parent-style-name", this.GetNamespaceUri("style")); 
     styleParentStyleName.Value = "Default"; 
     sheetNode.Attributes.Append(styleParentStyleName); 

     XmlAttribute styleDataStyleName = ownerDocument.CreateAttribute("style:data-style-name", this.GetNamespaceUri("style")); 
     styleDataStyleName.Value = "N19"; 
     sheetNode.Attributes.Append(styleDataStyleName); 

     sheetsRootNode.AppendChild(sheetNode); 
    } 

事實上,喬恩的建議是必要的,以及代碼完美工作。所以再次感謝喬恩。無論如何,一個黑色的,黑色的藝術確實... :)

1

這是可能,你只需要修復您的格式,因爲它目前破:

value.Value = Utils.DateTime(number).ToString("yyyy-MM-ddTHH:mm:ss"); 

變化:

  • 使用M代表一個月,沒有米(這意味着分鐘)
  • 使用HH小時,而不是hh(這是12小時制)

我建議指定inh CultureInfo.InvariantCulture以表明您不希望應用任何文化設置。

+0

感謝喬恩 - 不幸的是CultureInfo.InvariantCulture不起作用,你對格式的建議更改也不會。我有一種感覺,答案與OpenDocument XML屬性的黑色藝術有關。非常感謝您的幫助! – 2012-04-24 14:39:25

相關問題