【发布时间】:2017-01-18 09:41:48
【问题描述】:
我在使用 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 种样式的演示代码,我可能会更加了解它。
干杯, 乔赞特
【问题讨论】:
-
我的answer here 中有一个包含 3 种样式的示例 - 如果这有帮助,我会尝试找时间在这里写下答案。
标签: c# openxml docx openxml-sdk