2012-04-24 4 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); 
    } 

그러나, 나는 그들을 제대로 표시 얻을 수 없습니다. 더와 내가 발견 한 다양한 코드 조각을 가지고 노는 꽤을 썼다

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

:하지만, 이것은 내가 대신 지정된 날짜 형식의 셀에 "2012"을 표시하는, 지금까지없는 것입니다 성공. 오프 세트에, 거기에 어떤 관대 한 영혼이 있습니까?

감사합니다.

답변

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

실제로 코드가 완벽하게 작동하려면 Jon의 제안도 필요했습니다. 다시 Jon에게 감사드립니다. 어쨌든, 흑인, 흑인 예술 실제로 ... :)

1

그것은 당신이 단지는 현재 생겼습니다 주어진 당신의 형식을 수정해야 입니다 :

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

변경 : 월, 수 없습니다 m

  • 사용 M (분을 의미)
  • hh가 ​​아닌 hh를 사용하십시오 (12 시간 시계)

구체적인 문화 설정을 적용하고 싶지 않다는 것을 보여주기 위해 specifyinh CultureInfo.InvariantCulture을 제안 할 것입니다.

+0

감사합니다 Jon - 불행히도 CultureInfo.InvariantCulture가 작동하지 않으며 형식에 대한 제안 된 변경 사항도 없습니다. 대답은 OpenDocument XML 속성의 검은 기술과 관련이 있다는 느낌이 들었습니다. 여하튼 당신의 도움을 위해 많은 감사합니다! –