2013-07-29 19 views
5

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" }; 

Trả lời

7

Kỹ thuật của bạn sẽ hoạt động hoàn toàn nếu bạn đang chỉnh sửa tài liệu hiện có. Vấn đề là một tài liệu mới không có định nghĩa "Tiêu đề 1". Bạn sẽ phải thêm nó. Vì vậy, bạn có hai lựa chọn:

1. Làm việc với một mẫu tài liệu hiện có

Tạo một tài liệu mẫu (TemplatePath) để sử dụng như một cơ sở. Trong mã, sao chép nó vào đích cuối cùng (FinalPath) và thêm văn bản/bất cứ điều gì vào nó, áp dụng phong cách. Tiêu đề 1 đã có trong mẫu.

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" }); 

2. Tạo tài liệu mới của bạn từ đầu

Nếu bạn làm điều này, nó sẽ không có built-in phong cách. Vì vậy, tạo ra một phong cách, gọi nó là "Heading 1" và áp dụng nó vào đoạn văn của bạn.

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()); 

<sarcasm> See? OpenXML là một miếng bánh! </sarcasm > Tôi thề, đôi mắt của tôi đang lăn mạnh đến mức tôi đang bị đau đầu.

+1

StyleDefinitionPart phải là StyleDefinitionsPart –

0

(Sry, tiếng anh của tôi)

Tôi nghĩ rằng những cái tên kiểu là phụ thuộc ngôn ngữ của bạn, những gì sử dụng văn bản của bạn.

Heading 1 trong phong cách id tiếng Anh: "Heading 1" trong Hungarien: "Címsor 1" -> stlye id: "Cmsor1"

tôi thấy rằng, các tập tin docx xml phong cách.

Làm thế nào tôi Solove này:

  1. "sample.docx" đổi tên "sample.rar"
  2. Mở "sample.rar" bằng winrar.
  3. Mở thư mục "từ".
  4. Mở tệp "style.xml".
  5. Và tìm kiếm tên hoặc thuộc tính kiểu, những gì bạn cần.

Phân cấp kiểu là rất quan trọng!

Nó cũng phù hợp với kiểu bàn của tôi.