生成XML word标题
本文主要介绍如何使用xml生成docx文件。
要生成标题,需要知道标题的字体大小、颜色、是否有书签、RelationId等属性。
Word中有1-9个标题,一个标题在xml中用[Heading1]或[heading 1]标记。
让我们解析一个docx文件的标题,如下图1-1和图1-2所示。
图1-1
图1-2
文档文件中Val=3,对应styles中styleid = 3,name节点中val="heading 3 "。word就是这样生成标题的,我们可以根据word规则定义关系来设置相应的标题。文档结构的节点属性可以引用[。Net开发Word系列的XML节点关系],代码如下
公共分部类XMlWordDemoForm : Form
{
private int titleNum = 988888
私有字符串fontName = & # 34微软雅黑& # 34;;
//标题字体大小
私有字符串titleFontSize = & # 3424";
public XMlWordDemoForm()
{
initialize component();
}
私有void button1_Click(对象发送方,EventArgs e)
{
string fileName = @ & # 34HelloWord.docx & # 34;
使用(WordprocessingDocument wd = WordprocessingDocument。创建(文件名,WordprocessingDocumentType。文档))
{
var mainDocx = wd。AddMainDocumentPart();
var docx =新文档格式。open XML . word processing . document();
mainDocx。Document = docx
var body = mainDocx。Document.AppendChild(新文档格式。open XML . word processing . body());
AddTitle(body,& # 34;这是三级标题& # 34;, 3);
AddTitle(body,& # 34;这是一个四级标题& # 34;, 4);
AddTitle(body,& # 34;这是一个五级标题& # 34;, 5);
AppendText(body,& # 34;补充。用于测试正文内容的docx文档& # 34;);
styledefinitionspair part = mainDocx。StyleDefinitionsPart
if (part == null)
{
part = mainDocx。AddNewPart & ltStyleDefinitionsPart & gt("rId1 & # 34);
}
CreateDefinitionsPart(part);
mainDocx。Document.Save()。
过程。开始(文件名);
}
}
私有void AppendText(DocumentFormat。open XML . word processing . body body,字符串文本)
{
段落Paragraph = new Paragraph();
Run Run = new Run();
快跑。Append(新文本(文本));
段落。追加(运行);
身体。追加(段落);
}
private void add title(document format。open XML . word processing . body body,字符串文本,int级别)
{
title num++;
W.Paragraph Paragraph = new W . Paragraph();
W.paragraph properties paragraph properties = new W . paragraph properties();
W.ParagraphStyleId ParagraphStyleId = new W . ParagraphStyleId(){ Val = level。ToString()};
//修改段落
W.ParagraphMarkRunProperties ParagraphMarkRunProperties = new W . ParagraphMarkRunProperties();
//字体
W.run fonts run fonts 1 = new W . run fonts(){ Ascii = font name,HighAnsi = fontName,EastAsia = font name };
//字的大小
W.font size font size 1 = new W . FontSize(){ Val = word index . FontSize };
paragraphMarkRunProperties。append(run font S1);
paragraphMarkRunProperties。append(font size 1);
段落属性。append(paragraphStyleId);
段落属性。append(paragraphMarkRunProperties);
W.BookmarkStart BookmarkStart 1 = new W . BookmarkStart(){ Name = & # 34;_ Toc & # 34+标题编号,Id =标题编号。ToString()};
W.Run Run = new W . Run();
W.run properties run properties = new W . run properties();
W.run fonts run fonts = new W . run fonts(){ Hint = W . font typehintvalues . east Asia,Ascii = fontName,HighAnsi = fontName,east Asia = font name };
W.font size font size = new W . FontSize(){ Val = title FontSize };
运行属性。append(run fonts);
运行属性。append(fontSize);
W.lastrendedpagebreak LRP = new W . lastrendedpagebreak();
W.Text Text 1 = new W . Text();
文本1。Text =文本;
快跑。append(run properties);
快跑。追加(LRP);
快跑。Append(文本1);
W.BookmarkEnd BookmarkEnd 1 = new W . BookmarkEnd { Id = title num。ToString()};
段落。Append(段落属性);
段落。append(bookmarkstart 1);
段落。追加(运行);
段落。append(bookmarkend 1);
身体。追加(段落);
}
私有静态样式CreateTitleStyle(int titleIndex)
{
string titleID = titleIndex。ToString();
字符串rsid =字符串。空的;
字符串前=字符串。空的;
字符串后=字符串。空的;
字符串行=字符串。空的;
string val =字符串。空的;
int outline = title index-1;
if (titleIndex == 3)
{
rsid = & # 3400BA1E93 & # 34;
之前= & # 34;130";//"260"
之后= & # 34;0";
line = & # 34286";//"416"
val = & # 3432";
}
else if (titleIndex == 4)
{
rsid = & # 3400BA1E94 & # 34;
之前= & # 34;88";
之后= & # 34;0";
line = & # 34288";//"376"
val = & # 3428";
}
else if (titleIndex == 5)
{
rsid = & # 3400BA1E95 & # 34;
之前= & # 34;88";
之后= & # 34;0";
line = & # 34290";
val = & # 3424";
}
else if (titleIndex == 6)
{
rsid = & # 3400BA1E96 & # 34;
之前= & # 34;88";
之后= & # 34;0";
line = & # 34292";
val = & # 3420";
}
else if (titleIndex == 7)
{
rsid = & # 3400BA1E97 & # 34;
之前= & # 34;88";
之后= & # 34;0";
line = & # 34294";
val = & # 3416";
}
else if (titleIndex == 8)
{
rsid = & # 3400BA1E98 & # 34;
之前= & # 34;88";
之后= & # 34;0";
line = & # 34296";
val = & # 3414";
}
else if (titleIndex == 9)
{
rsid = & # 3400BA1E99 & # 34;
之前= & # 34;88";
之后= & # 34;0";
line = & # 34298";
val = & # 3412";
}
Style Style 2 = new Style(){ Type = Style values。Paragraph,style id = title id };
style name style name 2 = new style name(){ Val = & # 34;标题& # 34;+title id };
based on based on 1 = new based on(){ Val = & # 34;一& # 34;};
NextParagraphStyle NextParagraphStyle 1 = new NextParagraphStyle(){ Val = & # 34;一& # 34;};
linked style linked style 1 = new linked style(){ Val = titleID+& # 34;Char & # 34};
ui priority ui priority 1 = new ui priority(){ Val = 9 };
PrimaryStyle PrimaryStyle 2 = new PrimaryStyle();
Rsid Rsid 2 = new Rsid(){ Val = Rsid };
风格2。append(style name 2);
风格2。append(based on 1);
风格2。append(nextparagraphstyle 1);
风格2。append(linked style 1);
风格2。append(uipriority 1);
风格2。append(primary style 2);
风格2。append(rsid 2);
StyleParagraphProperties StyleParagraphProperties 2 = new StyleParagraphProperties();
keep next keep next 1 = new keep next();
keep lines keep lines 1 = new keep lines();
spacing between lines spacing between lines 1 = new spacing between lines(){ Before = Before,After = after,Line = line,LineRule = LineSpacingRuleValues。auto };
outline level outline level 1 = new outline level(){ Val = outline };
styleParagraphProperties2。append(keep next 1);
styleParagraphProperties2。append(keeplines 1);
styleParagraphProperties2。append(spacing between lines 1);
styleParagraphProperties2。append(outline level 1);
风格2。append(styleparagraphproperties 2);
StyleRunProperties StyleRunProperties 1 = new StyleRunProperties();
Bold Bold 1 = new Bold();
bold complex script bold complex script 1 = new bold complex script();
//Kern Kern 2 = new Kern(){ Val =(uint 32)44U };
font size font size 2 = new FontSize(){ Val = Val };
FontSizeComplexScript FontSizeComplexScript 2 = new FontSizeComplexScript(){ Val = Val };
styleRunProperties1。追加(bold 1);
styleRunProperties1。append(bold complexscript 1);
//styleRunProperties1。append(kern 2);
styleRunProperties1。append(font size 2);
styleRunProperties1。append(fontsizecomplexscript 2);
风格2。append(stylerunproperties 1);
返回样式2;
}
private void CreateDefinitionsPart(document format。open XML . packaging . styledefinitionspar styledefinitionspar)
{
W.Styles Styles 1 = new W . Styles();
样式1。addnamespacedideclaration(& # 34;r & # 34, "http://schemas . openxml formats . org/office document/2006/relationships & # 34;);
样式1。addnamespacedideclaration(& # 34;w & # 34, "http://schemas . open XML formats . org/wordprocessingml/2006/main & # 34;);
style heading style 3 = CreateTitleStyle(3);
style heading style 4 = CreateTitleStyle(4);
style heading style 5 = CreateTitleStyle(5);
样式1。append(heading style 3);
样式1。append(heading style 4);
样式1。append(heading style 5);
styleDefinitionsPart。Styles = styles1
}
}
备注:
在文档中
styles < w:style w:type = & # 34;段落& # 34;& gt指示段落的样式,它对应于
