2013-07-29 58 views
5

任何人都可以指導我如何使用開放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" }; 

回答

7

如果您正在編輯現有文檔,則您的技術完全可行。問題是新文檔沒有預定義的「標題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" }); 

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

<諷刺>看到了嗎? OpenXML是小菜一碟! < /諷刺>我發誓,我的眼睛滾得如此厲害,我頭痛。

+1

StyleDefinitionPart應該是StyleDefinitionsPart –

0

(Sry基因,我的英語)

我覺得風格的名字是取決於你的語言,有什麼用你的話。

標題1中的英式風格ID:在Hungarien 「標題1」 : 「Címsor1」 - > stlye ID: 「Cmsor1」

我看到的是,DOCX XML樣式文件。

我如何solove這樣:

  1. 「sample.docx」 重命名 「sample.rar」
  2. 打開 「sample.rar」 用WinRAR。
  3. 打開「word」文件夾。
  4. 打開「style.xml」文件。
  5. 並搜索樣式名稱或屬性,你需要什麼。

樣式層次結構非常重要!

它也適用於我的餐桌風格。