I am having issues with Office Open XML. I have been able to take some code off the internet and make a document with Hello World
and also add a heading to the document, however, other than that, I am having no success.
We are moving OOXML since Microsoft.Office.Interop.Word
takes too long for the clients and in my experience not a lot of people put Office in a server environment.
Basically, I create an object array with 7 Properties per object.
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; }
}
My current code strips through some files and populates this object array called businessRules
.
Now that I have all the rules, I need to spit them out into a .docx
. The created document needs to have 6 styles; Heading 1, Heading 2, Heading 3, etc...
The code below is my attempt to create the styles that I need before I start writing the different items to the document. Some things are commented out in an attempt to make it work. It also has some repeated code to try and get around some errors. I know this is some really bad code so I apologise to your eyes and brains in advance.
//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();
}
If someone even has some demo code that uses 3 styles I might be able to get my head around it a bit more.
Cheers, JohZant
I worked this out ages ago, but I forgot about this question. OOPS!
I will be honest, I did cheat. Like, a lot. But I'm not trying to reinvent the wheel here.
The Open XML SDK Tool (https://www.microsoft.com/en-au/download/details.aspx?id=30425) allowed me to upload a docx that I created manually with all the styles I needed, and then it gave me the C# to use to create the document programmatically.