我们都知道, Office 是基于 COM 组件技术开发的,属于非托管程序,然而 C# 使用的都是托管程序,那么如何使用非托管的 COM 组件就是我们操作 WORD 的第一个问题。所幸的是, .NET FRAMEWORK 提供了一种不同种类类库的转换工具 tlbimp,exe ,通过这个转换工具,我们可以把 COM 组件转化为 .NET FRAMEWORK 可以直接调用的 DLL 文件。
接下来就是转化工作了, Office 组件都可以在 C:Program FilesMicrosoft Office 目录下找到,当然安装的 Office 版本不同,子目录是不一样的。笔者使用的是 Office 2007 ,可以在 C:Program FilesMicrosoft OfficeOffice12 目录下找到 MSWORD.OLB ,这个是 WORD 组件的类库文件,还可以找到 MSACC.OLB 操作 ACCESS , MSPPT.OLB 操作 PPT , XL5CHS32.OLB 操作 EXCEL 。载入不同的组件就可以完成对不同 Office 组件的操作。使用 tlbimp,exe 工具转化 MSWORD.OLB 文件后可以得到三个 DLL 文件, Office ,dll , Visual BasicIDE.dll , Word .dll 。最后在编译文件的时候,记得将这三个 DLL 文件载入,命令如下:
csc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /r:office .dll /r:vbide.dll /r:word .dll word .cs
笔者使用 Visual Studio.NET 2005 编译环境,通过 IDE 提供的功能可以大大简化我们对组件转化的工作,并且在编译时也不需要输入那么繁杂的语句,非常方便了。下面介绍一下 IDE 载入 Office 组件的方式。
在菜单栏选择“项目”-“添加引用”,弹出的窗口中我们可以选择“ COM ”选项卡,找到 Microsoft Office 12.0 Object Library ( Office 2003/2007 需要使用 12.0 版的,如果你使用的是 Office 2000 或者更低的版本,只要载入 10.0 版的就可以了),确定后引入 . 也可以在“浏览”选项卡下找到我们上面提到的 MSWORD.OLB 文件,引入即可。引入后我们可以发现在解决方案中,引用目录下多了三个文件 Microsoft .Office .Core , Microsoft .Office .Interop .Word , VSIDE 。这说明引用文件成功,之后在编译程序的时候,在 Debug 目录下会生成两个 DLL 文件, Interop .Microsoft .Office .Core.dll , Interop .Microsoft .Office .Interop .Word .dll 。完成组件的引入,下面就可以开始程序设计了。
先看一下 Word 对像模型
Application
:用来表现
WORD
应用程序,包含其它所有对象。他的成员经常应用于整个
WORD
,你可以用它的属性和方法控制
WORD
环境。
Document
:
Document
对象是
WORD
编程的核心。当你打开一个已有的文档或创建一个新的文档时,就创建了一个新的
Document
对象,
新创建的
Document
将会被添加到
Word
Documents Collection
。
Selection
:
Selection
对象是描述当前选中的区域。若选择区域为空,则认为是当前光标处。
Rang
:是
Document
的连续部分,根据起始字符和结束字符定义位置。
Bookmark
:类似于
Rang
,但
Bookmark
可以有名字并在保存
Document
时
Bookmark
也被保存。
在编程中使用到的代码如下,注释比较详细,这里就不再具体的说明。
//Word
程序对象
private
Microsoft
.Office
.Interop
.Word
.Application
WordApp = new
Microsoft
.Office
.Interop
.Word
.Application
();
//Word
文档对象
private
Microsoft
.Office
.Interop
.Word
._Document
aDoc;
private
void
openfile_Click(object
sender, EventArgs
e)
{//
打开Word
文件
if
(openFileDialog.ShowDialog() == DialogResult
.OK)
{
//
定义打开文件的16
个参数
object
fileName = openFileDialog.FileName; //
文件名称
object
ConfirmConversions = false
; //
允许转换
object
ReadOnly = false
; //
只读方式打开
object
AddToRecentFiles = false
; //
添加到最近打开的文档
object
PasswordDocument = System.Type
.Missing;
object
PasswordTemplate = System.Type
.Missing;
object
Revert = System.Type
.Missing;
object
WritePasswordDocument = System.Type
.Missing;
object
WritePasswordTemplate = System.Type
.Missing;
object
Format = System.Type
.Missing; //
格式
object
Encoding = System.Type
.Missing; //
编码
object
Visible = System.Type
.Missing;
object
OpenAndRepair = System.Type
.Missing;
object
DocumentDirection = System.Type
.Missing;
object
NoEncodingDialog = System.Type
.Missing;
object
XMLTransform = System.Type
.Missing;
WordApp.Visible = true
;
try
{
//
打开文档
aDoc = WordApp.Documents.Open(ref
fileName, ref
ConfirmConversions, ref
ReadOnly, ref
AddToRecentFiles,
ref
PasswordDocument, ref
PasswordTemplate, ref
Revert, ref
WritePasswordDocument, ref
WritePasswordTemplate,
ref
Format, ref
Encoding, ref
Visible, ref
OpenAndRepair, ref
DocumentDirection, ref
NoEncodingDialog, ref
XMLTransform);
//
激活文档,使文档为当前处理
aDoc.Activate();
}
catch
{
MessageBox
.Show("
出现错误!"
);
}
}
}
private
void
closefile_Click(object
sender, EventArgs
e)
{//
关闭Word
文件
object
SaveChanges = false
; //
保存更改
object
OriginalFormat = System.Type
.Missing;
object
RouteDocument = System.Type
.Missing;
//
关闭文档
aDoc.Close(ref
SaveChanges, ref
OriginalFormat, ref
RouteDocument);
//
退出程序
WordApp.Quit(ref
SaveChanges, ref
OriginalFormat, ref
RouteDocument);
}
通过文档类对象aDoc
还可以完成文件的保存,另存为等等操作,详细的可以参阅MSDN
。