2011-08-22 75 views
1

我在文档占位符中添加了一些文本。本文由几个字符串组成,分割为“<line>”。我怎样才能用段落代替这段文字,每段只包含一个字符串?从字符串数组创建docx段落


+0

问题已解决=) – a1exis

回答

1

我找到了解决方案。只需要中断字符串并为每个字符串创建一个带格式的段落,否则元素将创建为OpenXmlUnknownElement。

XDocument customXml = GenerateXmlForReport(report); 
      String customXmlId = AddCustomXml(document, customXml); 
      DataBind(document, customXml, customXmlId); 
      document.MainDocumentPart.Document.Body.GetFirstChild<SdtBlock>().RemoveAllChildren(); 
      string[] lines = Regex.Split(report.ReportTextBody, "</line>"); 
      foreach (var line in lines) 
      { 
       Paragraph p = new Paragraph(); 
       ParagraphProperties paragraphProperties1 = new ParagraphProperties(); 
       ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "BodyText" }; 
       ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties(); 
       RunFonts runFonts1 = new RunFonts() { Ascii = "Arial", HighAnsi = "Arial" }; 
       paragraphMarkRunProperties1.Append(runFonts1); 
       paragraphProperties1.Append(paragraphStyleId1); 
       paragraphProperties1.Append(paragraphMarkRunProperties1); 
       RunProperties runProperties1 = new RunProperties(); 
       RunStyle runStyle1 = new RunStyle() { Val = "PlaceholderText" }; 

       runProperties1.Append(runStyle1); 
       Run run = new Run(); 
       Text txt = new Text(line); 
       run.Append(txt); 
       p.Append(run); 
       document.MainDocumentPart.Document.Body.GetFirstChild<SdtBlock>().Append(p); 

      }