繁体   English   中英

使用多个样式创建Office Open XML文档

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM