Ai có thể hướng dẫn tôi cách thêm kiểu định sẵn trên đoạn bằng cách sử dụng Xử lý văn bản XML mở không? Tôi đã thử các giải pháp khác nhau có sẵn trên diễn đàn nhưng không có gì phù hợp với tôi. Dưới đây là những gì tôi muốn hoàn thành:OpenXML Thêm kiểu đoạn văn (Heading1, Heading2, Head 3 Etc) vào tài liệu xử lý văn bản
// 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 phải là StyleDefinitionsPart –