任何人都可以指導我如何使用開放XML文字處理在段落上添加預定義樣式?我已經嘗試過論壇上提供的各種解決方案,但沒有爲我工作。這是我想要完成的:OpenXML將段落樣式(標題1,標題2,標題3等)添加到文字處理文檔
// 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" };
StyleDefinitionPart應該是StyleDefinitionsPart –