vb.net OpenXml 编辑 word 文件标题中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19007385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:13:57  来源:igfitidea点击:

OpenXml Edit text in the header of a word file

asp.netvb.netopenxml

提问by invictus1306

I'm using Open XML and I should change the text in the header of a word file. To change a specific paragraph in the document I have used the following code:

我正在使用 Open XML,我应该更改 word 文件标题中的文本。要更改文档中的特定段落,我使用了以下代码:

Dim body = wdDoc.MainDocumentPart.Document.Body
            Dim paras = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
            Dim header = body.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Header)()


            For Each para In paras
                For Each run In para.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Run)()
                    For Each testo In run.Elements(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
                        If (testo.Text.Contains("<$doc_description$>")) Then
                            testo.Text = testo.Text.Replace("<$doc_description$>", "replaced-text")
                        End If
                    Next
                Next
            Next

thanks in advance!

提前致谢!

回答by Hans

You can use the following code to replace a tag in the header of a word document:

您可以使用以下代码替换word文档页眉中的标签:

Using wdDoc = WordprocessingDocument.Open("header.docx", True)

  For Each headerPart In wdDoc.MainDocumentPart.HeaderParts
    For Each currentParagraph In headerPart.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Paragraph)()
      For Each currentRun In currentParagraph.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Run)()
        For Each currentText In currentRun.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()

          If (currentText.Text.Contains("$doc-description$")) Then
            Console.WriteLine("found")
            currentText.Text = currentText.Text.Replace("$doc-description$", "replaced-text")
          End If

        Next
      Next
    Next
  Next

End Using

First, enumerate all HeaderPartsof the word document. Then search for all Textelements containing the tag to replace. Then replace the tag with your text.

首先,枚举所有HeaderParts的word文档。然后搜索Text包含要替换的标记的所有元素。然后用您的文本替换标签。

Please note that you should use a tag without <>and _characters. If your tag contains these characters then word splits the text among multiple Textelements.

请注意,您应该使用不带<>_字符的标签。如果您的标签包含这些字符,则 word 会在多个Text元素之间拆分文本。

If you want to change the text in a table (or in any other element) just search for all Textelements:

如果要更改表格(或任何其他元素)中的文本,只需搜索所有Text元素:

Using wdDoc = WordprocessingDocument.Open("header.docx", True)

  For Each headerPart In wdDoc.MainDocumentPart.HeaderParts
    For Each currentText In headerPart.RootElement.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()

      If (currentText.Text.Contains("$doc-description$")) Then
        Console.WriteLine("found")
        currentText.Text = currentText.Text.Replace("$doc-description$", "replaced-text")
      End If

    Next
  Next

End Using

回答by BrunoMartinsPro

Ported to C# from Hans answer!

从 Hans 的答案移植到 C#!

 //Gets all the headers
 foreach (var headerPart in doc.MainDocumentPart.HeaderParts)
 {
      //Gets the text in headers
      foreach(var currentText in headerPart.RootElement.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>())
      {
          currentText.Text = currentText.Text.Replace("[Thanks]", "Thanks");
      }
 }

回答by invictus1306

thanks for the reply actually works :) I also tried with the following code:

感谢您的回复实际上有效:) 我也尝试使用以下代码:

    For Each headref In mainDoc.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.HeaderReference)()
    headerRelationshipId = headref.Id.Value
    headerType = headref.Type.Value.ToString()
    Dim header01 As DocumentFormat.OpenXml.Wordprocessing.Header = DirectCast(wdDoc.MainDocumentPart.GetPartById(headerRelationshipId), HeaderPart).Header
    Dim headerText As New StringBuilder()

    For Each text00 As DocumentFormat.OpenXml.Wordprocessing.Text In header01.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)()
        If (text00.Text.Contains("")) Then
            text00.Text = text00.Text.Replace("", "replaced-text")
        End If
    Next
Next

But if I wanted to change the text in a table (instead of a paragraph)?

但是,如果我想更改表格(而不是段落)中的文本?