2011-08-22 76 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); 

      }