从字符串数组创建 docx 段落

发布于 2024-11-30 16:02:28 字数 99 浏览 0 评论 0 原文

我在文档占位符中包含一些文本。该文本由多个字符串组成,由“”分隔。如何用一系列段落替换该文本,每个段落仅包含一个字符串?


I have in document placeholder with some text. This text consists from several strings, splitted by "<line>". How can I replace this text witha scoupe of paragraphs, each containing only one string?


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白昼 2024-12-07 16:02:28

我找到了解决方案。只需要断开字符串并为每个字符串创建一个带格式的段落,否则元素将创建为 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);

            }                

I've found a solution. It was only necessary to break the string and for each string create a paragraph with formatting, otherwise the elements are created as 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);

            }                
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文