创建具有多种样式的 Office Open XML 文档

Creating an Office Open XML document with mutliple styles

我在使用 Office Open XML 时遇到问题。我已经能够从互联网上获取一些代码并使用 Hello World 制作文档并向文档添加标题,但是,除此之外,我没有成功。

我们正在移动 OOXML,因为 Microsoft.Office.Interop.Word 对客户端来说花费的时间太长,而且根据我的经验,没有多少人将 Office 放在服务器环境中。

基本上,我创建了一个 object 数组,每个 object 有 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 的 object 数组。

既然我已经有了所有的规则,我需要把它们吐出到一个.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 种样式的演示代码,我也许可以更深入地了解它。

干杯, 乔赞特

我很久以前就解决了这个问题,但我忘记了这个问题。糟糕!

老实说,我确实作弊了。喜欢,很多。但我并不是要在这里重新发明轮子。

Open XML SDK 工具 (https://www.microsoft.com/en-au/download/details.aspx?id=30425) 允许我上传我手动创建的包含我需要的所有样式的 docx,然后它给了我 C# 用于创建以编程方式记录。