2016-07-06 65 views
0

与合并字段中的所有文本我有两个“DEMO”字我怎么能替换文件

,我想替换成MERGEFIELD

只是一个“DEMO”用我的代码改变...

我怎样才能取代文本与字段?

感谢

 Application app = new Application(); 
     Document word = new Document(); 

     word = app.Documents.Add(ref path, ref missing, ref missing, ref missing); 

     object objType = WdFieldType.wdFieldMergeField; 

     object hashVal = null; 
     Hashtable hash = new Hashtable(); 

     hash.Add("DEMO", "mergefield11111"); 


     foreach (object s in hash.Keys) 
     { 

      Range rng = app.ActiveDocument.Content; 
      Find findObject = app.Selection.Find; 

      object ff = s; 
      hashVal = hash[s];  

      findObject.Text = ff.ToString(); 
      findObject.ClearFormatting(); 
      if (rng.Find.Execute(ref ff, 
      ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
      ref missing, ref missing, ref missing, WdReplace.wdReplaceOne, ref missing, ref missing, 
      ref missing, ref missing)) 
      { 
       Field field = app.Selection.Fields.Add(app.Selection.Range, ref objType, ref hashVal, ref missing); 
      } 
     } 
     app.Documents.Open("test1.docx"); 
+0

如果你被封锁了,你可以试试我创建的库,就是这样做的(替换值,或循环和条件:https://github.com/open-xml-templating/docxtemplater) – edi9999

回答

0

你只是在做一个Find操作。请尝试while (rng.Find.Execute(...)) { ... }

+0

它非常非常非常感谢你的作品 – Hee

0

您可以使用Open XML SDK 2.5。您可以将Office文件读取为Big XML文件,然后您可以更改所需内容。

  1. 您必须阅读的Word文件的MemoryStream
public byte[] DocumentGenerator(String templatePath) 
    { 
     byte[] buffer; 
     using (MemoryStream stream = new MemoryStream()) 
     { 
      buffer = System.IO.File.ReadAllBytes(templatePath); 
      stream.Write(buffer, 0, buffer.Length); 
      using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, true)) 
      { 
       //Get list bookmarks 
       var listBookMarks = wordDocument.MainDocumentPart.Document.Descendants<BookmarkStart>().ToList(); 
       //Get list Merge Fields 
       var listMergeFields = wordDocument.MainDocumentPart.Document.Descendants<FieldCode>().ToList(); 
       //Do some code 
       wordDocument.Close(); 
      } 
      buffer = stream.ToArray(); 
     } 

     return buffer; 
    } 
  • 然后,您可以更改文本,添加段落,添加表或其他。你可以用你的Office文件做所有事情。 (Word,Excel ...) 这是我的例子。我试着在我的书签字段中添加项目符号列表:
  •   if (bookmark != null) 
         { 
          var parent = bookmark.Parent; // bookmark's parent element 
          var childEllement = parent.Elements<Run>().Last(); 
          var lastChild = childEllement.LastChild; 
          childEllement.RemoveChild(lastChild); 
    
    
          foreach (var itemChild in groupList) 
          { 
    
           Paragraph paragraph1 = new Paragraph(); 
           ParagraphProperties paragraphProperties = new ParagraphProperties(); 
           ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "ListParagraph" }; 
           Tabs tabs = new Tabs(); 
           TabStop tabStop1 = new TabStop() { Val = TabStopValues.Left, Position = 284 }; 
    
           tabs.Append(tabStop1); 
           SpacingBetweenLines spacingBetweenLines1 = new SpacingBetweenLines() { After = "120", Line = "360", LineRule = LineSpacingRuleValues.Auto }; 
           Indentation indentation1 = new Indentation() { Left = "0", FirstLine = "0" }; 
           Justification justification = new Justification() { Val = JustificationValues.Both }; 
    
           ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties(); 
    
           paragraphProperties.Append(paragraphStyleId1); 
           paragraphProperties.Append(tabs); 
           paragraphProperties.Append(spacingBetweenLines1); 
           paragraphProperties.Append(indentation1); 
           paragraphProperties.Append(justification); 
           paragraphProperties.Append(paragraphMarkRunProperties1); 
           Run run1 = new Run(); 
           RunProperties runProperties1 = new RunProperties(); 
    
           Bold bold2 = new Bold(); 
           FontSizeComplexScript fontSizeComplexScript2 = new FontSizeComplexScript() { Val = "24" }; 
    
           runProperties1.Append(bold2); 
           runProperties1.Append(fontSizeComplexScript2); 
           Text text1 = new Text(); 
           text1.Text = "- " + itemChild + ";"; 
           run1.Append(runProperties1); 
           run1.Append(text1); 
           paragraph1.Append(paragraphProperties); 
           paragraph1.Append(run1); 
           parent.InsertAfterSelf(paragraph1); 
          } 
         } 
    
  • 你可以选择你想要什么用Open XML SDK 2.5 Productivity Tool这项计划将帮助您了解文件结构中读取任意文件。 祝你好运!