简体   繁体   English

使用OpenXML和C#处理word文档

[英]Processing word document using OpenXML and C#

So I'm trying to populate the content controls in a word document by matching the Tag and populating the text within that content control. 所以我试图通过匹配Tag并填充该内容控件中的文本来填充word文档中的内容控件。

The following displays in a MessageBox all of the tags I have in my document. 下面在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);
    }
}

However when I try the following it doesn't work. 但是,当我尝试以下操作时,它不起作用。

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

My objective in the end is if I match the appropriate tag I want to populate/change the text in the content control that tag belongs to. 我的目标是,如果我匹配相应的标签,我想填充/更改标签所属的内容控件中的文本。

So I basically used FirstChild and InnerXml to pick apart the documents XML contents. 所以我基本上使用FirstChild和InnerXml来分离文档XML内容。 From there I developed the following that does what I need. 从那里我开发了以下功能,满足我的需求。

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

This finds all the tags inside of your document and stores the elements in a list 这将查找文档中的所有标记,并将这些元素存储在列表中

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

This part of the code starts off at the tag part of the xml. 这部分代码从xml的标记部分开始。 From there I call parent twice to jump up two levels in the XML code so I can gain access to the Run level using descendants. 从那里我调用parent两次在XML代码中跳过两个级别,这样我就可以使用后代访问Run级别。

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

And last but not least the following code stores a new value into the text part of the PlainText Content control. 最后但并非最不重要的是,以下代码将一个新值存储到PlainText Content控件的文本部分。

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

Things that I noticed is the xml hierarchy is a funky thing. 我注意到的事情是xml层次结构是一个时髦的事情。 I find it easier to access these things from bottom up, hence why I started with the tags and moved up. 我发现从下到上更容易访问这些东西,因此我开始使用标签并向上移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM