谁能指导我如何使用开放的 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" };