OpenXml在word文件的标题中编辑文本

时间:2013-09-25 14:14:01

标签: asp.net vb.net openxml

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

提前感谢!

3 个答案:

答案 0 :(得分:7)

您可以使用以下代码替换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元素 包含要替换的标记。然后用您的文字替换标签。

请注意,您应使用不带<>_个字符的代码。如果你的 标签包含这些字符,然后单词将文本分割为多个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

答案 1 :(得分:3)

从汉斯接听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 :(得分:0)

感谢回复确实有效:) 我还尝试使用以下代码:

    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

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