spire.doc组件为用户提供了多种域类型选择,其中涵盖了word自带的各种域类型。这些域类型被定义在枚举spire.doc.fieldtype中,下图为部分域类型及描述:
该文我们详细介绍如何使用spire.doc添加下面三种常见的word 域:fielddate(日期域),fieldif (条件域)及toc(目录域)。
fielddate (日期域)
c#
//新建一个word文档对象
document document = new document();
//添加一个section和paragraph
section section = document.addsection();
paragraph paragraph = section.addparagraph();
//添加文本到paragraph
paragraph.appendtext("今天的日期为: ");
//添加日期域并设置日期格式
field field = paragraph.appendfield("date", fieldtype.fielddata) as field;
field.code = @"date \@" "\"yyyy年mm月dd日 \"";
//保存文档
document.savetofile("sample.docx", fileformat.docx2013);
vb.net
'新建一个word文档对象
dim document as document = new document
'添加一个section和paragraph
dim section as section = document.addsection
dim paragraph as paragraph = section.addparagraph
'添加文本到paragraph
paragraph.appendtext("今天的日期为: ")
'添加日期域并设置日期格式
dim field as field = ctype(paragraph.appendfield("date", fieldtype.fielddata),field)
field.code = ("date \@" """yyyytmmdd """"")
'保存文档
document.savetofile("sample.docx", fileformat.docx2013)
fieldif(条件域)
spire.doc创建条件域的时候,我们会用到两个域,一个是条件域,一个是邮件合并域。格式为这样{if { mergefield count } > "60" "成绩合格" "成绩不合格"}
c#
{
//新建一个word文档对象并添加section和paragraph
document document = new document();
section section = document.addsection();
paragraph paragraph = section.addparagraph();
//调用createiffield方法并赋值
createiffield(document, paragraph);
string[] fieldname = { "count" };
string[] fieldvalue = { "100" };
//合并值到条件域
document.mailmerge.execute(fieldname, fieldvalue);
//更新域
document.isupdatefields = true;
//保存文档
document.savetofile("sample.docx", fileformat.docx);
}
static void createiffield(document document, paragraph paragraph)
{
//添加条件域并设置条件
iffield iffield = new iffield(document);
iffield.type = fieldtype.fieldif;
iffield.code = "if ";
paragraph.items.add(iffield);
paragraph.appendfield("count",fieldtype.fieldmergefield);
paragraph.appendtext(" > ");
paragraph.appendtext("\"60\" ");
paragraph.appendtext("\"成绩合格\" ");
paragraph.appendtext("\"成绩不合格\"");
iparagraphbase end = document.createparagraphitem(paragraphitemtype.fieldmark);
(end as fieldmark).type = fieldmarktype.fieldend;
paragraph.items.add(end);
iffield.end = end as fieldmark;
}
vb.net
'新建一个word文档对象并添加section和paragraph
dim document as document = new document
dim section as section = document.addsection
dim paragraph as paragraph = section.addparagraph
'调用createiffield方法并赋值
createiffield(document, paragraph)
dim fieldname() as string
dim fieldvalue() as string
'合并值到条件域
document.mailmerge.execute(fieldname, fieldvalue)
'更新域
document.isupdatefields = true
'保存文档
document.savetofile("sample.docx", fileformat.docx)
private shared sub createiffield(byval document as document, byval paragraph as paragraph)
'添加条件域并设置条件
dim iffield as iffield = new iffield(document)
iffield.type = fieldtype.fieldif
iffield.code = "if "
paragraph.items.add(iffield)
paragraph.appendfield("count", fieldtype.fieldmergefield)
paragraph.appendtext(" > ")
paragraph.appendtext("""60"" ")
paragraph.appendtext("\"成绩合格\" ")
paragraph.appendtext("\"成绩不合格\"")
dim end as iparagraphbase = document.createparagraphitem(paragraphitemtype.fieldmark)
ctype(end,fieldmark).type = fieldmarktype.fieldend
paragraph.items.add(end)
iffield.end = ctype(end,fieldmark)
end sub
toc(目录域)
spire.doc提供了appendtoc()方法让用户能够直接添加目录域。
c#
{
//新建一个word文档对象并添加section和paragraph
document document = new document();
section section = document.addsection();
paragraph paragraph = section.addparagraph();
//添加目录域
paragraph.appendtoc(1, 3);
//添加内容到段落并设置样式
paragraph para1 = section.addparagraph();
para1.appendtext("head1");
para1.applystyle(builtinstyle.heading1);
paragraph para2 = section.addparagraph();
para2.appendtext("head2");
para2.applystyle(builtinstyle.heading2);
//更新目录域
document.updatetableofcontents();
//保存文档
document.savetofile("toc.docx", fileformat.docx);
}
vb.net
'新建一个word文档对象并添加section和paragraph
dim document as document = new document
dim section as section = document.addsection
dim paragraph as paragraph = section.addparagraph
'添加目录域
paragraph.appendtoc(1, 3)
'添加内容到段落并设置样式
dim para1 as paragraph = section.addparagraph
para1.appendtext("head1")
para1.applystyle(builtinstyle.heading1)
dim para2 as paragraph = section.addparagraph
para2.appendtext("head2")
para2.applystyle(builtinstyle.heading2)
'更新目录域
document.updatetableofcontents
'保存文档
document.savetofile("toc.docx", fileformat.docx)