OpenXML将段落样式(Heading1,Heading2,Head 3 Etc)添加到文字处理文档中

Nab*_*eel 6 c# word-automation openxml-sdk

任何人都可以指导我如何使用开放式XML文字处理在段落上添加预定义样式吗?我在论坛上尝试了各种解决方案,但对我来说没什么用.这是我想要完成的:

                // Create a document by supplying the filepath. 
                WordprocessingDocument wordDocument = WordprocessingDocument.Create("E:/Test/Executive.Docx", WordprocessingDocumentType.Document);

                // Add a main document part. 
                MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

                // Create the document structure and add some text.
                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());
                Paragraph para = body.AppendChild(new Paragraph());

                Run run = para.AppendChild(new Run());
                run.AppendChild(new Text("Executive Summary"));
                if (para.Elements<ParagraphProperties>().Count() == 0)
                    para.PrependChild<ParagraphProperties>(new ParagraphProperties());

                // Get the ParagraphProperties element of the paragraph.
                ParagraphProperties pPr = para.Elements<ParagraphProperties>().First();

                // Set the value of ParagraphStyleId to "Heading3".
                pPr.ParagraphStyleId = new ParagraphStyleId() { Val = "Heading1" };
Run Code Online (Sandbox Code Playgroud)

Rap*_*Rap 8

如果您正在编辑现有文档,那么您的技术将完全奏效.问题是新文档没有预定义的"标题1".你必须添加它.所以你有两个选择:

1.使用现有模板文档

创建模板文档(TemplatePath)以用作基础.在代码中,将其复制到最终目标(FinalPath)并添加文本/任何内容,应用样式.标题1已经在模板中.

if (File.Exists(FinalPath))
  File.Delete(FinalPath);
File.Copy(TemplatePath, FinalPath);
WordprocessingDocument wordDocument = WordprocessingDocument.Open(FinalPath, true);
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Executive Summary"));
para.ParagraphProperties = new ParagraphProperties(new ParagraphStyleId() { Val="Heading1" });
Run Code Online (Sandbox Code Playgroud)

2.从头开始创建新文档

如果这样做,它将没有内置样式.因此,创建一个样式,称之为"标题1"并将其应用于您的段落.

WordprocessingDocument wordDocument = WordprocessingDocument.Create(FinalPath, WordprocessingDocumentType.Document);
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Executive Summary"));
StyleDefinitionPart styleDefinitionsPart = wordDocument.AddStylesDefinitionPart();
Styles styles = styleDefinitionsPart.Styles;
Style style = new Style() {
  Type = StyleValues.Paragraph, 
  StyleId = styleid, 
  CustomStyle = true
};
StyleName styleName1 = new StyleName() { Val = "Heading1" };
style.Append(styleName1);
StyleRunProperties styleRunProperties1 = new StyleRunProperties();
styleRunProperties1.Append(new Bold);
styleRunProperties1.Append(new Italic());
styleRunProperties1.Append(new RunFonts() { Ascii = "Lucida Console" };);
styleRunProperties1.Append(new FontSize() { Val = "24" });  // Sizes are in half-points. Oy!
style.Append(styleRunProperties1);
styles.Append(style);
pPr.ParagraphStyleId = new ParagraphStyleId(){ Val = "Heading1" };
para.PrependChild<ParagraphProperties>(new ParagraphProperties());
Run Code Online (Sandbox Code Playgroud)

<讽刺>查看?OpenXML是小菜一碟!</ sarcasm>我发誓,我的眼睛滚得很厉害,我头疼.

  • StyleDefinitionPart应该是StyleDefinitionsPart (2认同)
  • 什么是 styleid?pPr 也未初始化。这不是一个好的答案。 (2认同)