【问题标题】:OpenXml Edit text in the header of a word fileOpenXml 编辑 word 文件标题中的文本
【发布时间】:2013-09-25 14:14:00
【问题描述】:

我正在使用 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

提前致谢!

【问题讨论】:

    标签: asp.net vb.net openxml


    【解决方案1】:

    从汉斯的答案移植到 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");
          }
     }
    

    【讨论】:

    • 我正在为特定页面的唯一标题应用分节符。如何更改特定页面的标题?
    【解决方案2】:

    您可以使用以下代码替换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
    

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

    请注意,您应该使用不带&lt;&gt;_ 字符的标签。如果你的 标签包含这些字符,然后 word 将文本拆分为多个 Text 元素。

    如果您想更改表格(或任何其他元素)中的文本,只需搜索 对于所有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
    

    【讨论】:

    • @andrea85:我更新了我的答案以显示如何替换表格中包含的文本。如果我的回答对您有帮助,请通过点击答案左侧的空心箭头接受/投票。
    【解决方案3】:

    感谢您的回复确实有效:) 我还尝试了以下代码:

    对于 mainDoc.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.HeaderReference)() 中的每个 headref headerRelationshipId = headref.Id.Value headerType = headref.Type.Value.ToString() 暗淡 header01 作为 DocumentFormat.OpenXml.Wordprocessing.Header = DirectCast(wdDoc.MainDocumentPart.GetPartById(headerRelationshipId), HeaderPart).Header 将 headerText 调暗为新 StringBuilder() 对于每个 text00 作为 DocumentFormat.OpenXml.Wordprocessing.Text 在 header01.Descendants(Of DocumentFormat.OpenXml.Wordprocessing.Text)() 如果 (text00.Text.Contains("")) 那么 text00.Text = text00.Text.Replace("", "替换文本") 万一 下一个 下一个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多