2009-05-29 70 views
0

我想爲我的老闆在C#應用程序中創建一個報告生成器,我遇到了這個頁面並查看了RichTextBoxes,並認爲我可以基於這個想法做我的老闆正在尋找的東西。 http://openxmldeveloper.org/articles/OpenXMLDocFromDotNet.aspx在.NET中創建一個Open XML文件 - 架構

我正在運行的問題是他們假定您在Office 2007測試版中創建應用程序的XML部分的示例代碼。這裏列出的模式對零售Office 2007不起作用。任何人都可以告訴我我可以在哪裏找到更多關於模式的一般信息,或者解釋代碼在這裏做什麼?或者,如果任何人有基於富文本框的內容創建.docx文件的不同建議,將不勝感激。我發現不同的資源,提供類似這樣的建議:http://nishantrana.wordpress.com/2007/11/03/creating-word-document-using-c/

但我一直有問題得到它來識別WordApp是什麼。

下面是與架構問題的第一個鏈接的代碼。

private void GenerateDocument_Click(object sender, EventArgs e) 
{ 
    string _nameSpaceURI = "http://schemas.microsoft.com/office/word/2005/10/wordml"; 
    string docFileName = GetSavePath(); 

    //-- Step 1 - Creating the document xml 
    XmlDocument doc = new XmlDocument(); 
    XmlElement _wWordDoc = doc.CreateElement("w:wordDocument", _nameSpaceURI); 
    doc.AppendChild (_wWordDoc); 
    XmlElement _wbody = doc.CreateElement("w:body",_nameSpaceURI); 
    _wWordDoc.AppendChild(_wbody); 
    // Check if the string contains a line feed 
    string[] _SplitStr = mleTextForDocument.Text.Split('\n'); 
    // if it contains line feed then each entry with a line feed goes to a new paragraph. 
    for (int row = 0; row < _SplitStr.Length; row++) 
    { 
     XmlElement _wp1 = doc.CreateElement("w:p",_nameSpaceURI); 
     _wbody.AppendChild(_wp1); 
     XmlElement _wr1 = doc.CreateElement("w:r", _nameSpaceURI); 
     _wp1.AppendChild(_wr1); 
     XmlElement _wt11 = doc.CreateElement("w:t", _nameSpaceURI); 
     _wr1.AppendChild(_wt11); 
     XmlNode _wt1 = doc.CreateNode(XmlNodeType.Text, "w:t",_nameSpaceURI); 
     _wt1.Value = _SplitStr[row]; 
     _wt11.AppendChild(_wt1); 
    } 

    //-- Step 2 - Creating the Package 
    Package package = null; 
    package = Package.Open(docFileName, FileMode.Create, FileAccess.ReadWrite); 

    //-- Step 3 - Create the main document part (document.xml) 
    Uri uri = new Uri("/word/document.xml", UriKind.Relative); 
    PackagePart part = package.CreatePart(uri, "application/vnd.ms-word.main+xml"); 
    StreamWriter partWrt = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)); 
    doc.Save(partWrt); 
    partWrt.Close(); 
    package.Flush(); 

    //-- Step 4 - Create the relationship file 
    uri = new Uri("/word/document.xml", UriKind.Relative); 
    PackageRelationship rel = package.CreateRelationship(uri, TargetMode.Internal, "http://schemas.microsoft.com/office/2006/relationships/officeDocument", "rId1"); 
    package.Flush(); 

    //-- Step 5- Close the document. 
    package.Close(); 
} 

我是缺乏明確的問題抱歉,但我真的不知道要問什麼問題。我以前從未使用過架構,從未使用過XML,也從未在之前添加對我的項目的引用。任何意見或建議表示讚賞。

+0

是否必須另存爲.docx?將直接.rtf服務項目的需求?甚至是一個簡單的文本文件? – 2009-05-29 19:14:49

+0

需要某種文檔文檔,應用程序將創建涉及某些數據的圖形,這些數據將插入到應用程序的RichTextBox中 – user84613 2009-05-29 19:26:07

回答

3

雖然這個令人懷疑的問題,顯然它來自我bizzaro邪惡的雙胞胎(nwonknu)(elgoog),笑話正確。

無論如何,我已經說過before,我會再說一遍質量提醒的XML /的OpenXML的來源是Eric White。他是一個非常活躍的博主,看起來像4年以上的一致貼子(有時候好的源代碼會蒸發掉),無論如何都能在他的博客中輕鬆幾分鐘,我相信你對OpenXML + Linq 2 XML的掌握將會有點兒更堅實。