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

从ASP.NET得到Microsoft Word文档的代码

程序员文章站 2024-03-07 22:28:57
背景 自动化(automation)是一个过程,它允许编程语言譬如visual basic.net或c#写的应用程序可以编程控制其它应用程序。自动化到word允许你执行像创...
背景
自动化(automation)是一个过程,它允许编程语言譬如visual basic.net或c#写的应用程序可以编程控制其它应用程序。自动化到word允许你执行像创建新文档,向文档中添加文本,邮件合并,还有控制文档格式这样的操作。使用word和其它microsoft office应用程序,几乎所有你能在用户面板上手动实现的操作都可以通过自动化编程实现。word通过一个对象模型来实现这个编程功能性(programmatically functionality)。对象模型是一系列类和方法,它们提供和word的逻辑组成部分相似的服务。例如,一个应用程序对象,一个文档对象,和一个段落对象,这些每个都包含着word的相应组成部分的功能性。

项目
在.net中操作word的第一步,你需要在你的项目中添加一个com引用,这通过右击解决方案窗口中的引用->添加引用。单击com标签寻找microsoft word 10.0 object library。单击“选择”添加,单击“确定”返回。
这会自动在你的应用程序文件夹中放置一个程序集(assembly)将com接口邦定到word。
现在,你可以生成一个word应用程序的实例了。

word.applicationclass owordapp = new word.applicationclass();
你可以调用microsoft word提供给你的很有趣的方法和属性来操作word格式的文档。学习怎样操纵word,excel和powerpoint对象模型最好的方法就是,在这些office应用程序中使用宏录制器:
1、在“工具”菜单中的“宏”选项中选择“录制新宏”,然后执行你感兴趣的任务。
2、在“工具”菜单中的“宏”选项中选择“停止录制”。
3、一旦你完成了录制,选择“工具”菜单中的“宏”选项下的“宏”,选择你录制的宏,单击“编辑”。
这将将你带入生成的vba代码,这些代码完成了你记录的任务。注意记录下的宏在多数情况下不是最好的代码,但它提供了一个快速和可用的例子。
例如要打开一个存在的文件加入一些文本:
复制代码 代码如下:

object filename = "c:\\database\\test.doc";
object;
object isvisible = true;
object missing = system.reflection.missing.value;
word.applicationclass owordapp = new word.applicationclass();
word.document oworddoc = owordapp.documents.open(ref filename,
ref missing,ref readonly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref isvisible,
ref missing,ref missing,ref missing);
oworddoc.activate();
owordapp.selection.typetext("this is the text");
owordapp.selection.typeparagraph();
oworddoc.save();
owordapp.application.quit(ref missing, ref missing, ref missing);

或者要打开一个新的文档然后保存它:
复制代码 代码如下:

word.applicationclass owordapp = new word.applicationclass();
word.document oworddoc = owordapp.documents.add(ref missing,
ref missing,ref missing, ref missing);
oworddoc.activate();
owordapp.selection.typetext("this is the text");
owordapp.selection.typeparagraph();
oworddoc.saveas("c:\\myfile.doc");
owordapp.application.quit(ref missing, ref missing, ref missing);

在c#中,word文档类的open方法定义为:open(ref object, ref object, ref object, ref object, ref object, ref object,ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object, ref object)。这说明c#的open方法接受15个必要参数,每个参数都必须以ref关键字为前缀而且每个参数都必须是object类型的。因为第一个参数是一个文件名,通常在visual basic.net中的一个string值,我们必须声明一个object类型的变量来保存c#的string值,代码如下:

object filename = "c:\\database\\test.doc";
尽管我们在open方法中只需要使用第一个参数,但是记住c#不允许可选参数,所以我们以object类型的变量的形式提供余下的14个参数,它们保存system.reflection.missing.value的值。

使用模板
如果你使用自动化来创建都是一致格式的文档,使用预定义模板来处理新的文档将会很方便。在你的word自动化客户程序中使用模板与不用模板相比,有两个显著的优点:
·对于你文档的格式和对象位置上你可以拥有更多的控制权
·你可以使用更少的代码来建立你的文档
通过使用模板,你可以调整文档中表格、段落还有其它对象的位置,还有也可以调整这些对象的格式。通过使用自动化,你可以创建一个基于你的模板的文档,而只用如下的代码:

word.applicationclass owordapp = new word.applicationclass();
object otemplate = "c:\\mytemplate.dot";
oworddoc = owordapp.documents.add(ref otemplate,
ref missing,ref missing, ref missing);
在你的模版中,你可以定义书签,这样你的自动化客户程序可以在文档中的特定位置填入可变的文本,如下:

object obookmark = "mybookmark";
oworddoc.bookmarks.item(ref obookmark).range.text = "some text here";
使用模板的另一个优点是你可以创建你希望在运行时应用的存储格式风格,如下:

object ostylename = "mystyle";
oworddoc.bookmarks.item(ref obookmark).range.set_style(ref ostylename);
使用ccwordapp类
这个项目包含一个文件:ccwordapp.cs。我不想每次都写代码来插入文本,打开一个文件,等等……所以我决定写一个ccwordapp类来包括多数重要的方法。下面是对这个类和它的方法的简要描述。
复制代码 代码如下:

public class ccwordapp
{
//it's a reference to the com object of microsoft word application
private word.applicationclass owordapplic;
// it's a reference to the document in use
private word.document oworddoc;
// activate the interface with the com object of microsoft word
public ccwordapp();
// open an existing file or open a new file based on a template
public void open( string strfilename);
// open a new document
public void open( );
// deactivate the interface with the com object of microsoft word
public void quit( );
// save the document
public void save( );
//save the document with a new name as html document
public void saveas(string strfilename );
// save the document in html format
public void saveashtml(string strfilename );
// insert text
public void inserttext( string strtext);
// insert line break
public void insertlinebreak( );
// insert multiple line break
public void insertlinebreak( int nline);
// set the paragraph alignment
// possible values of strtype :"centre", "right", "left", "justify"
public void setalignment(string strtype );
// set the font style
// possible values of strtype :"bold","italic,"underlined"
public void setfont( string strtype );
// disable all the style
public void setfont( );
// set the font name
public void setfontname( string strtype );
// set the font dimension
public void setfontsize( int nsize );
// insert a page break
public void insertpagebreak();
// go to a predefined bookmark
public void gotobookmark( string strbookmarkname);
// go to the end of document
public void gototheend( );
// go to the beginning of document
public void gotothebeginning( );

这样,打开一个已有的文件的操作就是:
ccwordapp test ;
test = new ccwordapp();
test.open ("c:\\database\\test.doc");
test.inserttext("this is the text");
test.insertlinebreak;
test.save ();
test.quit();
细节
示例项目包括:
ccwordapp.cs - the class
createdocmodel.aspx: 创建一个基于模板的文档和使用书签的例子。
createnewdoc.aspx: 创建一个文档和插入一些文本的例子。
modifydocument.aspx: 打开一个已有文档然后在后面添加一些文本的例子。
template\template1.dot: 一个模板的例子(在createdocmodel.aspx中使用)。
记住一点,你保存文件的目录必须是可写的。请查看web.config文件来修改路径。
引用
microsoft word objects
converting microsoft office vba macros to visual basic .net and c#
howto: automate microsoft word to perform a mail merge from visual basic .net
a primer to the office xp primary interop assemblies
howto: find and use office object model documentation
creating and opening microsoft word documents from .net using c#