2016-10-05 48 views
1

一個的Office Open XML文檔。我已經能夠採取一些代碼了互聯網,使文檔與Hello World,也是一個標題添加到文檔,但是,除此之外,我沒有成功。創建與我有的Office Open XML問題複式風格

我們正在移動OOXML,因爲Microsoft.Office.Interop.Word對於客戶端來說花費的時間太長,而且根據我的經驗,很多人並未將Office放在服務器環境中。

基本上,創建與每個對象7個屬性的對象陣列。通過一些文件

public class BusinessRule 
{ 
    public string NAME { get; set; } 
    public string PATH { get; set; } 
    public string LEVEL { get; set; } 
    public string DESCRIPTION { get; set; } 
    public string[] ROUTINGRULES { get; set; } 
    public string[] FILENAMINGRULES { get; set; } 
    public string[] ADDITIONALBUSINESSMETADATA { get; set; } 
} 

我當前的代碼條並填充此稱爲businessRules對象陣列。

現在,我有所有的規則,我需要將它們吐出成.docx。創建的文檔需要6種樣式;標題1,標題2,標題3,等...

下面的代碼是我嘗試創建一個我需要之前,我開始寫不同的項目文檔的樣式。爲了使其發揮作用,有些內容被註釋掉了。它也有一些重複的代碼來嘗試解決一些錯誤。我知道這是一些非常糟糕的代碼,所以我提前向你的眼睛和大腦道歉。

//return styles 
    private Style[] getStyle() 
    { 
     List<Style> styles = new List<Style>(); 

     //initialise objects 
     RunProperties rPrH1 = new RunProperties(); 
     RunProperties rPrH2 = new RunProperties(); 
     RunProperties rPrH3 = new RunProperties(); 
     RunProperties rPrH4 = new RunProperties(); 
     RunProperties rPrH5 = new RunProperties(); 
     RunProperties rPrH6 = new RunProperties(); 
     RunProperties rPrN = new RunProperties(); 




     Color[] color = new Color[3]; 
     color[0] = new Color(); 
     color[0].Val = "4F81BD"; 
     color[1] = new Color(); 
     color[1].Val = "144E85"; 
     color[2] = new Color(); 
     color[2].Val = "000000"; 

     RunFonts rFont = new RunFonts(); 
     rFont.Ascii = "Calibri Light"; // the font is Arial 
     rPrH1.Append(rFont); 
     rFont = new RunFonts(); 
     rFont.Ascii = "Calibri Light"; // the font is Arial 
     rPrH2.Append(rFont); 
     rFont = new RunFonts(); 
     rFont.Ascii = "Calibri Light"; // the font is Arial 
     rPrH3.Append(rFont); 
     rFont = new RunFonts(); 
     rFont.Ascii = "Calibri Light"; // the font is Arial 
     rPrH4.Append(rFont); 
     rFont = new RunFonts(); 
     rFont.Ascii = "Calibri Light"; // the font is Arial 
     rPrH5.Append(rFont); 
     rFont = new RunFonts(); 
     rFont.Ascii = "Calibri Light"; // the font is Arial 
     rPrH6.Append(rFont); 
     rFont = new RunFonts(); 
     rFont.Ascii = "Calibri Light"; // the font is Arial 
     rPrN.Append(rFont); 

     //Add heading 1 
     //4F81BD - Calibri Light - 16 
     //creation of a style 
     Style H1 = new Style(); 
     H1.StyleId = "Heading1"; //this is the ID of the style 
     H1.Append(new Name() { Val = "Heading 1" }); //this is name         
                // our style based on Normal style 
     H1.Append(new BasedOn() { Val = "Heading1" }); 
     // the next paragraph is Normal type 
     H1.Append(new NextParagraphStyle() { Val = "Heading 5" }); 
     //run properties 
     //rPrH1.Append(color[0]); 
     //rPr.Append(new Bold()); // it is Bold 
     rPrH1.Append(new FontSize() { Val = "16" }); //font size (in 1/72 of an inch) 
     H1.Append(rPrH1); 

     //Add heading 2 
     //4F81BD - Calibri Light - 13 
     Style H2 = new Style(); 
     H2.StyleId = "Heading2"; //this is the ID of the style 
     H2.Append(new Name() { Val = "Heading 2" }); //this is name         
                // our style based on Normal style 
     H2.Append(new BasedOn() { Val = "Heading2" }); 
     // the next paragraph is Normal type 
     H2.Append(new NextParagraphStyle() { Val = "Heading 5" }); 
     //run properties 
     rPrH2.Append(color[0]); 
     rPrH2.Append(new FontSize() { Val = "13" }); //font size (in 1/72 of an inch) 
     H2.Append(rPrH2); 

     //Add heading 3 
     //144E85 - Calibri Light - 12 
     Style H3 = new Style(); 
     H3.StyleId = "Heading3"; //this is the ID of the style 
     H3.Append(new Name() { Val = "Heading 3" }); //this is name         
                // our style based on Normal style 
     H3.Append(new BasedOn() { Val = "Heading3" }); 
     // the next paragraph is Normal type 
     H3.Append(new NextParagraphStyle() { Val = "Heading 5" }); 
     //run properties 
     rPrH3.Append(color[1]); 
     rPrH3.Append(new FontSize() { Val = "12" }); //font size (in 1/72 of an inch) 
     H3.Append(rPrH3); 

     //Add heading 4 
     //144E85 - Calibri Light - 11 
     Style H4 = new Style(); 
     H4.StyleId = "Heading4"; //this is the ID of the style 
     H4.Append(new Name() { Val = "Heading 4" }); //this is name         
                // our style based on Normal style 
     H4.Append(new BasedOn() { Val = "Heading4" }); 
     // the next paragraph is Normal type 
     H4.Append(new NextParagraphStyle() { Val = "Heading 5" }); 
     //run properties 
     rPrH4.Append(color[1]); 
     rPrH4.Append(new FontSize() { Val = "11" }); //font size (in 1/72 of an inch) 
     H4.Append(rPrH4); 

     //Add heading 5 
     //4F81BD - Calibri Light - 11 
     Style H5 = new Style(); 
     H5.StyleId = "Heading5"; //this is the ID of the style 
     H5.Append(new Name() { Val = "Heading 5" }); //this is name         
                // our style based on Normal style 
     H5.Append(new BasedOn() { Val = "Heading5" }); 
     // the next paragraph is Normal type 
     //H5.Append(new NextParagraphStyle() { Val = "Normal" }); 
     //run properties 
     rPrH5.Append(color[0]); 
     rPrH5.Append(new FontSize() { Val = "11" }); //font size (in 1/72 of an inch) 
     H5.Append(rPrH5); 

     //Add heading 6 
     //144E85 - Calibri Light - 11 
     Style H6 = new Style(); 
     H6.StyleId = "Heading6"; //this is the ID of the style 
     H6.Append(new Name() { Val = "Heading 6" }); //this is name         
                // our style based on Normal style 
     H6.Append(new BasedOn() { Val = "Heading6" }); 
     // the next paragraph is Normal type 
     //H6.Append(new NextParagraphStyle() { Val = "Normal" }); 
     //run properties 
     rPrH6.Append(color[1]); 
     rPrH6.Append(new FontSize() { Val = "11" }); //font size (in 1/72 of an inch) 
     H6.Append(rPrH6); 

     //Add normal 
     //000000 - Calibri Light - 11 
     Style N = new Style(); 
     H6.StyleId = "Normal"; //this is the ID of the style 
     H6.Append(new Name() { Val = "Normal" }); //this is name         
                // our style based on Normal style 
     H6.Append(new BasedOn() { Val = "Normal" }); 
     //run properties 
     rPrN.Append(color[2]); 
     rPrN.Append(new FontSize() { Val = "11" }); //font size (in 1/72 of an inch) 
     N.Append(rPrN); 

     return styles.ToArray(); 
    } 

如果有人甚至有一些使用3種風格的演示代碼,我可能會多花一點腦筋。

乾杯, JohZant

+0

我在我的[這裏有答案] 3樣式的例子(http://stackoverflow.com/questions/25056927/unable-to-use-existing-paragraph-styles-in-open-xml/25058393#25058393 ) - 如果這有幫助,我會盡力在這裏找到時間寫一個答案。 – petelids

回答

2

很久以前我工作了這一點,但我忘了這個問題。 OOPS!

我會說實話,我沒騙。喜歡,很多。但我不想在這裏重新發明輪子。

Open XML SDK工具(https://www.microsoft.com/en-au/download/details.aspx?id=30425)允許我上傳手動創建的docx,並使用我需要的所有樣式手動創建,然後它給了我用C#編程創建文檔。