本文介绍.NET操作Word的实现方法。通过Word的对象库文件“MSWORD.OLB”(word 2000为MSWORD9.OLB),就可以实现这个功能。
.NET操作Word可以用using Word来完成。基本上,vs.net将会主动将 库文件转化为DLL组件,这样咱们只要在源码中创立该组件目标即可到达操作Word的意图。
要完成,咱们就需要Word的目标库文件“MSWORD.OLB”(word 2000为MSWORD9.OLB),一般装置了Office Word后,你就可以在office装置目录的Office10文件夹下面找到这个文件,当咱们将这个文件引进到项目后,咱们就可以在源码中运用各种操作函数来操作Word。具体做法是翻开菜单栏中的项目>增加引证>阅读,在翻开的“挑选组件”对话框中找到MSWORD.OLB后按确认即可引进此目标库文件。
在CS代码文件中对命名空间的运用,如:using Word;.NET操作Word典范如下:
- usingSystem;
- usingSystem.Drawing;
- usingSystem.Collections;
- usingSystem.ComponentModel;
- usingSystem.Windows.Forms;
- usingWord;
- namespaceExamSecure
- {
- ///
- ///ItemToDoc的摘要阐明。
- ///
- publicclassItemToDoc:System.Windows.Forms.Form
- {
- objectstrFileName;
- ObjectNothing;
- Word.ApplicationClassmyWordApp=newWord.ApplicationClass();
- Word.DocumentmyWordDoc;
- stringstrContent="";
- privateSystem.ComponentModel.Containercomponents=null;
- publicItemToDoc()
- {
- //
- //Windows窗体规划器支撑所必需的
- //
- InitializeComponent();
- //
- //TODO:在InitializeComponent调用后增加任何结构函数代码
- //
- }
- [STAThread]
- staticvoidMain()
- {
- System.Windows.Forms.Application.Run(newItemToDoc());
- }
- ///
- ///整理一切正在运用的资源。
- ///
- protectedoverridevoidDispose(booldisposing)
- {
- if(disposing)
- {
- if(components!=null)
- {
- components.Dispose();
- }
- }
- base.Dispose(disposing);
- }
- #regionWindowsFormDesignergeneratedcode
- ///
- ///规划器支撑所需的办法-不要运用代码修改器修正
- ///此办法的内容。
- ///
- privatevoidInitializeComponent()
- {
- //
- //ItemToDoc
- //
- this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
- this.ClientSize=newSystem.Drawing.Size(292,273);
- this.Name="ItemToDoc";
- this.Text="ItemToDoc";
- this.Load+=newSystem.EventHandler(this.ItemToDoc_Load);
- }
- #endregion
- privatevoidItemToDoc_Load(objectsender,System.EventArgse)
- {
- WriteFile();
- }
- privatevoidWriteFile()
- {
- strFileName=System.Windows.Forms.Application.StartupPath+"\\试题库【"+GetRandomString()+"】.doc";
- ObjectNothing=System.Reflection.Missing.Value;
- myWordDoc=myWordApp.Documents.Add(refNothing,refNothing,refNothing,refNothing);
- #region将数据库中读取得数据写入到word文件中
- strContent="试题库\n\n\r";
- WriteFile(strContent);
- strContent="试题库";
- WriteFile(strContent);
- #endregion
- //将WordDoc文档目标的内容保存为DOC文档
- myWordDoc.SaveAs(refstrFileName,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);
- //封闭WordDoc文档目标
- myWordDoc.Close(refNothing,refNothing,refNothing);
- //封闭WordApp组件目标
- myWordApp.Quit(refNothing,refNothing,refNothing);
- }
- ///
- ///获取一个随即字符串
- ///
- ///
- privatestringGetRandomString()
- {
- DateTimeiNow=DateTime.Now;
- stringstrDate=iNow.ToString("yyyyMMddHHmmffff");
- Randomran=newRandom();
- intiRan=Convert.ToInt32(10000*ran.NextDouble());
- stringstrRan=iRan.ToString();
- //位数缺乏则补0
- intiRanlen=strRan.Length;
- for(inti=0;i<4-iRanlen;i++)
- {
- strRan="0"+strRan;
- }
- returnstrDate+strRan;
- }
- ///
- ///将字符串写入到Word文件中
- ///
- ///要写入的字符串
- privatevoidWriteFile(stringstr)
- {
- myWordDoc.Paragraphs.Last.Range.Text=str;
- }
- }
- }
以上便是.NET操作Word的完成代码。
【修改引荐】
- ASP.NET新手问题总结
- 深入研究Repeater控件:最大的灵活性
- DataList控件入门介绍
- DataGrid Web控件运作机制探秘
- 小议ASP.NET数据Web控件之间的相似性
转载请说明出处
知优网 » .NET操作Word的完成:using Word(.net 生成word)
知优网 » .NET操作Word的完成:using Word(.net 生成word)