欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#生成word记录实例解析

程序员文章站 2024-02-19 09:44:10
本文以实例形式讲述了c#生成word记录的方法,具体实现代码如下: private void button1_click(object sender, syste...

本文以实例形式讲述了c#生成word记录的方法,具体实现代码如下:

private void button1_click(object sender, system.eventargs e)
{
object omissing = system.reflection.missing.value;
object oendofdoc = "\\endofdoc";

/* \endofdoc是预定义的bookmark */ 

//创建一个document.
word._application oword;
word._document odoc;
oword = new word.application();
oword.visible = true;
odoc = oword.documents.add(ref omissing, ref omissing,
ref omissing, ref omissing);

//在document的开始部分添加一个paragraph.
word.paragraph opara1;
opara1 = odoc.content.paragraphs.add(ref omissing);
opara1.range.text = "heading 1";
opara1.range.font.bold = 1;
opara1.format.spaceafter = 24;    //24 pt spacing after paragraph.
opara1.range.insertparagraphafter();

//在当前document的最后添加一个paragraph

word.paragraph opara2;
object orng = odoc.bookmarks.get_item(ref oendofdoc).range;
opara2 = odoc.content.paragraphs.add(ref orng);
opara2.range.text = "heading 2";
opara2.format.spaceafter = 6;
opara2.range.insertparagraphafter();

//接着添加一个paragraph
word.paragraph opara3;
orng = odoc.bookmarks.get_item(ref oendofdoc).range;
opara3 = odoc.content.paragraphs.add(ref orng);
opara3.range.text = "this is a sentence of normal text. now here is a table:";
opara3.range.font.bold = 0;
opara3.format.spaceafter = 24;
opara3.range.insertparagraphafter();

//添加一个3行5列的表格,填充数据,并且设定第一行的样式

word.table otable;
word.range wrdrng = odoc.bookmarks.get_item(ref oendofdoc).range;
otable = odoc.tables.add(wrdrng, 3, 5, ref omissing, ref omissing);
otable.range.paragraphformat.spaceafter = 6;
int r, c;
string strtext;
for(r = 1; r <= 3; r++)
for(c = 1; c <= 5; c++)
{
strtext = "r" + r + "c" + c;
otable.cell(r, c).range.text = strtext;
}
otable.rows[1].range.font.bold = 1;
otable.rows[1].range.font.italic = 1;

//接着添加一些文字

word.paragraph opara4;
orng = odoc.bookmarks.get_item(ref oendofdoc).range;
opara4 = odoc.content.paragraphs.add(ref orng);
opara4.range.insertparagraphbefore();
opara4.range.text = "and here's another table:";
opara4.format.spaceafter = 24;
opara4.range.insertparagraphafter();


//添加一个5行2列的表,填充数据并且改变列宽
wrdrng = odoc.bookmarks.get_item(ref oendofdoc).range;
otable = odoc.tables.add(wrdrng, 5, 2, ref omissing, ref omissing);
otable.range.paragraphformat.spaceafter = 6;
for(r = 1; r <= 5; r++)
for(c = 1; c <= 2; c++)
{
strtext = "r" + r + "c" + c;
otable.cell(r, c).range.text = strtext;
}
otable.columns[1].width = oword.inchestopoints(2); //change width of columns 1 & 2
otable.columns[2].width = oword.inchestopoints(3);

//keep inserting text. when you get to 7 inches from top of the
//document, insert a hard page break.
object opos;
double dpos = oword.inchestopoints(7);
odoc.bookmarks.get_item(ref oendofdoc).range.insertparagraphafter();
do
{
wrdrng = odoc.bookmarks.get_item(ref oendofdoc).range;
wrdrng.paragraphformat.spaceafter = 6;
wrdrng.insertafter("a line of text");
wrdrng.insertparagraphafter();
opos = wrdrng.get_information
              (word.wdinformation.wdverticalpositionrelativetopage);
}
while(dpos >= convert.todouble(opos));
object ocollapseend = word.wdcollapsedirection.wdcollapseend;
object opagebreak = word.wdbreaktype.wdpagebreak;
wrdrng.collapse(ref ocollapseend);
wrdrng.insertbreak(ref opagebreak);
wrdrng.collapse(ref ocollapseend);
wrdrng.insertafter("we're now on page 2. here's my chart:");
wrdrng.insertparagraphafter();

//添加一个chart

word.inlineshape oshape;
object oclasstype = "msgraph.chart.8";
wrdrng = odoc.bookmarks.get_item(ref oendofdoc).range;
oshape = wrdrng.inlineshapes.addoleobject(ref oclasstype, ref omissing, 
ref omissing, ref omissing, ref omissing,
ref omissing, ref omissing, ref omissing);

//demonstrate use of late bound ochart and ochartapp objects to
//manipulate the chart object with msgraph.
object ochart;
object ochartapp;
ochart = oshape.oleformat.object;
ochartapp = ochart.gettype().invokemember("application",
bindingflags.getproperty, null, ochart, null);

//change the chart type to line.
object[] parameters = new object[1];
parameters[0] = 4; //xlline = 4
ochart.gettype().invokemember("charttype", bindingflags.setproperty,
null, ochart, parameters);

//update the chart image and quit msgraph.
ochartapp.gettype().invokemember("update",
bindingflags.invokemethod, null, ochartapp, null);
ochartapp.gettype().invokemember("quit",
bindingflags.invokemethod, null, ochartapp, null);
//... if desired, you can proceed from here using the microsoft graph 
//object model on the ochart and ochartapp objects to make additional
//changes to the chart.

//set the width of the chart.
oshape.width = oword.inchestopoints(6.25f);
oshape.height = oword.inchestopoints(3.57f);

//add text after the chart.
wrdrng = odoc.bookmarks.get_item(ref oendofdoc).range;
wrdrng.insertparagraphafter();
wrdrng.insertafter("the end.");

//close this form.
this.close();
}

使用模板生成通用格式word文件:

如果您要使用自动化功能创建的文档都是通用格式,则利用基于预设格式的模板的新文档来开始创建过程会更加容易。与从头创建文档相比,将某个模板与 word 自动化客户端配合使用有两大优点:
1.您可以对整个文档中的对象的格式设置和布局施加更多控制。
2.可以使用较少的代码创建文档。
通过使用模板,可以精确地调整表格、段落和其他对象在文档中的布局,并可为这些对象添加格式设置。通过使用自动化功能,可以基于包含下面这样的代码的模板创建新文档: 在模板中,可以定义书签,这样,自动化客户端就可以在文档的特定位置加入可变文本,如下所示: 使用模板的另一个优点在于,您可以创建和存储希望在运行时应用的格式样式,如下所示:

object otemplate = "c:\\mytemplate.dot";
odoc = oword.documents.add(ref otemplate, ref omissing,
 ref omissing, ref omissing);

object obookmark = "mybookmark";
odoc.bookmarks.item(ref obookmark).range.text = "some text here";

object ostylename = "mystyle";
odoc.bookmarks.item(ref obookmark).range.set_style(ref ostylename);

object ostylename = "mystyle";
oword.selection.set_style(ref ostylename);

最主要的就是理解word application 的框架层次,其它的就像面向过程编程一样,一步步写代码,其中比较麻烦的是嵌套的表格。