使用OpenXML和C#处理Word文档

3

我正在尝试通过匹配标签并填充内容控件中的文本来填充Word文档中的内容控件。

以下代码会在消息框中显示我文档中所有的标签。

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

我的最终目标是,如果我匹配了适当的标签,我想填充/更改该标签所属的内容控件中的文本。


你的代码有什么问题?bstring是空的吗? - Maxime Porté
@MaximePorté 我注意到当您没有正确提供文本字段的路径时,它无法处理。主要问题是某些内容控件与其他内容控件的XML不同。即使它们是相同类型的。这是由于文档结构的原因。 - cmircovich
1个回答

0

所以我基本上使用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代码中的两个级别,以便使用descendants访问Run级别。

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

最后但并非最不重要的是,以下代码将一个新值存储到PlainText内容控件的文本部分中。
run[0].GetFirstChild<Text>().Text = "<new_text_value>";

我注意到的是XML层次结构有点奇怪。我发现从下往上访问这些内容更容易,因此我从标签开始向上移动。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接