C操作Word代码实例Word下载.docx
《C操作Word代码实例Word下载.docx》由会员分享,可在线阅读,更多相关《C操作Word代码实例Word下载.docx(49页珍藏版)》请在冰豆网上搜索。
使用tlbimp,exe工具转化MSWORD.OLB文件后可以得到三个DLL文件,Office,dll,VisualBasicIDE.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.dllword.cs
笔者使用VisualStudio.NET2005编译环境,通过IDE提供的功能可以大大简化我们对组件转化的工作,并且在编译时也不需要输入那么繁杂的语句,非常方便了。
下面介绍一下IDE载入Office组件的方式。
在菜单栏选择“项目”-“添加引用”,弹出的窗口中我们可以选择“COM”选项卡,找到MicrosoftOffice12.0ObjectLibrary(Office2003/2007需要使用12.0版的,如果你使用的是Office2000或者更低的版本,只要载入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将会被添加到WordDocumentsCollection。
Selection:
Selection对象是描述当前选中的区域。
若选择区域为空,则认为是当前光标处。
Rang:
是Document的连续部分,根据起始字符和结束字符定义位置。
Bookmark:
类似于Rang,但Bookmark可以有名字并在保存Document时Bookmark也被保存。
在编程中使用到的代码如下,注释比较详细,这里就不再具体的说明。
//Word程序对象
privateMicrosoft.Office.Interop.Word.ApplicationWordApp=newMicrosoft.Office.Interop.Word.Application();
//Word文档对象
privateMicrosoft.Office.Interop.Word._DocumentaDoc;
privatevoidopenfile_Click(objectsender,EventArgse)
{//打开Word文件
if(openFileDialog.ShowDialog()==DialogResult.OK)
{
//定义打开文件的16个参数
objectfileName=openFileDialog.FileName;
//文件名称
objectConfirmConversions=false;
//允许转换
objectReadOnly=false;
//只读方式打开
objectAddToRecentFiles=false;
//添加到最近打开的文档
objectPasswordDocument=System.Type.Missing;
objectPasswordTemplate=System.Type.Missing;
objectRevert=System.Type.Missing;
objectWritePasswordDocument=System.Type.Missing;
objectWritePasswordTemplate=System.Type.Missing;
objectFormat=System.Type.Missing;
//格式
objectEncoding=System.Type.Missing;
//编码
objectVisible=System.Type.Missing;
objectOpenAndRepair=System.Type.Missing;
objectDocumentDirection=System.Type.Missing;
objectNoEncodingDialog=System.Type.Missing;
objectXMLTransform=System.Type.Missing;
WordApp.Visible=true;
try
//打开文档
aDoc=WordApp.Documents.Open(reffileName,refConfirmConversions,refReadOnly,refAddToRecentFiles,
refPasswordDocument,refPasswordTemplate,refRevert,refWritePasswordDocument,refWritePasswordTemplate,
refFormat,refEncoding,refVisible,refOpenAndRepair,refDocumentDirection,refNoEncodingDialog,refXMLTransform);
//激活文档,使文档为当前处理
aDoc.Activate();
}
catch
MessageBox.Show("
出现错误!
"
);
privatevoidclosefile_Click(objectsender,EventArgse)
{//关闭Word文件
objectSaveChanges=false;
//保存更改
objectOriginalFormat=System.Type.Missing;
objectRouteDocument=System.Type.Missing;
//关闭文档
aDoc.Close(refSaveChanges,refOriginalFormat,refRouteDocument);
//退出程序
WordApp.Quit(refSaveChanges,refOriginalFormat,refRouteDocument);
C#生成Word代码
1.c#操作word在指定书签插入文字或者图片
usingWord=Microsoft.Office.Interop.Word;
objectNothing=System.Reflection.Missing.Value;
objectformat=Word.WdSaveFormat.wdFormatDocument;
Word.ApplicationwordApp=newWord.ApplicationClass();
//打开网页选择内容
objectsrcFileName=@"
c:
\new1.doc"
;
//里面有图片
Word.DocumentwordDoc2=wordApp.Documents.Open(refsrcFileName,refformat,refNothing,refNothing,refNothing,refNothing,refNothing,