簡體   English   中英

使用OpenXML和C#處理word文檔

[英]Processing word document using OpenXML and C#

所以我試圖通過匹配Tag並填充該內容控件中的文本來填充word文檔中的內容控件。

下面在MessageBox中顯示我文檔中的所有標記。

//Create a copy of the template file and open the document
File.Delete(hhscDocument);
File.Copy(hhscTemplate, hhscDocument, true);

//Open the word document specified by location
using (var document = WordprocessingDocument.Open(hhscDocument, true))
{

    //Change the document type from template to document
    var mainDocument = document.MainDocumentPart.Document;
    if (mainDocument.Body.Descendants<Tag>().Any())
    {
        //MessageBox.Show(mainDocument.Body.Descendants<Table>().Count().ToString());
        var tags = mainDocument.Body.Descendants<Tag>().ToList();
        var aString = string.Empty;
        foreach(var tag in tags)
        {
            aString += string.Format("{0}{1}", tag.Val, Environment.NewLine);
        }
        MessageBox.Show(aString);
    }
}

但是,當我嘗試以下操作時,它不起作用。

//Create a copy of the template file and open the document
File.Delete(hhscDocument);
File.Copy(hhscTemplate, hhscDocument, true);

//Open the word document specified by location
using (var document = WordprocessingDocument.Open(hhscDocument, true))
{

    //Change the document type from template to document
    var mainDocument = document.MainDocumentPart.Document;
    if (mainDocument.Body.Descendants<Tag>().Any())
    {
        //MessageBox.Show(mainDocument.Body.Descendants<Table>().Count().ToString());
        var tags = mainDocument.Body.Descendants<Tag>().ToList();
        var bString = string.Empty;
        foreach(var tag in tags)
        {
            bString += string.Format("{0}{1}", tag.Parent.GetFirstChild<Text>().Text, Environment.NewLine);
        }
        MessageBox.Show(bString);
    }
}

我的目標是,如果我匹配相應的標簽,我想填充/更改標簽所屬的內容控件中的文本。

所以我基本上使用FirstChild和InnerXml來分離文檔XML內容。 從那里我開發了以下功能,滿足我的需求。

//Open the word document specified by location
using (var document = WordprocessingDocument.Open(hhscDocument, true))
{       
    var mainDocument = document.MainDocumentPart.Document;
    if (mainDocument.Body.Descendants<Tag>().Any())
    {
        //Find all elements(descendants) of type tag
        var tags = mainDocument.Body.Descendants<Tag>().ToList();

        //Foreach of these tags
        foreach (var tag in tags)
        {
            //Jump up two levels (.Parent.Parent) in the XML element and then jump down to the run level
            var run = tag.Parent.Parent.Descendants<Run>().ToList();

            //I access the 1st element because there is only one element in run
            run[0].GetFirstChild<Text>().Text = "<new_text_value>";
        }
    }
    mainDocument.Save();
}

這將查找文檔中的所有標記,並將這些元素存儲在列表中

var tags = mainDocument.Body.Descendants<Tag>().ToList();

這部分代碼從xml的標記部分開始。 從那里我調用parent兩次在XML代碼中跳過兩個級別,這樣我就可以使用后代訪問Run級別。

var run = tag.Parent.Parent.Descendants<Run>().ToList();

最后但並非最不重要的是,以下代碼將一個新值存儲到PlainText Content控件的文本部分。

run[0].GetFirstChild<Text>().Text = "<new_text_value>";

我注意到的事情是xml層次結構是一個時髦的事情。 我發現從下到上更容易訪問這些東西,因此我開始使用標簽並向上移動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM