[英]Creating an Office Open XML document with mutliple styles
我遇到Office Open XML問題。 我已經能夠從互聯網上獲取一些代碼並使用Hello World
創建文檔並在文檔中添加標題,但除此之外,我沒有成功。
我們正在移動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
我很久以前就把它弄出來了,但我忘記了這個問題。 OOPS!
我會說實話,我做過騙子。 喜歡,很多。 但我不是想在這里重新發明輪子。
Open XML SDK工具( https://www.microsoft.com/en-au/download/details.aspx?id=30425 )允許我上傳我手動創建的docx,其中包含我需要的所有樣式,然后它給了我用C#來編程創建文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.