2013-09-25 97 views
2

我正在使用Open XML,我應該更改文件頭文件中的文本。要更改文檔中的特定段落,我使用了以下代碼:OpenXml在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 

在此先感謝!

回答

6

您可以使用下面的代碼的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 

首先,枚舉字文檔的所有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 
+0

@ andrea85:我已更新我的答案,以顯示如何替換表中包含的文本。如果點擊答案左側的空心箭頭幫助你,請接受/提出我的答案。 – Hans

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 

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

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