.NET操作Word的實(shí)現(xiàn):using Word
.NET操作Word可以用using Word來實(shí)現(xiàn)。基本上,vs.net將會(huì)自動(dòng)將 庫文件轉(zhuǎn)化為DLL組件,這樣我們只要在源碼中創(chuàng)建該組件對(duì)象即可達(dá)到操作Word的目的。
要實(shí)現(xiàn),我們就需要Word的對(duì)象庫文件“MSWORD.OLB”(word 2000為MSWORD9.OLB),通常安裝了Office Word后,你就可以在office安裝目錄的Office10文件夾下面找到這個(gè)文件,當(dāng)我們將這個(gè)文件引入到項(xiàng)目后,我們就可以在源碼中使用各種操作函數(shù)來操作Word。具體做法是打開菜單欄中的項(xiàng)目>添加引用>瀏覽,在打開的“選擇組件”對(duì)話框中找到MSWORD.OLB后按確定即可引入此對(duì)象庫文件。
在CS代碼文件中對(duì)命名空間的應(yīng)用,如:using Word;.NET操作Word范例如下:
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using Word;
- namespace ExamSecure
- {
- ///
- /// ItemToDoc 的摘要說明。
- ///
- public class ItemToDoc : System.Windows.Forms.Form
- {
- object strFileName;
- Object Nothing;
- Word.ApplicationClass myWordApp=new Word.ApplicationClass();
- Word.Document myWordDoc;
- string strContent="";
- private System.ComponentModel.Container components = null;
- public ItemToDoc()
- {
- //
- // Windows 窗體設(shè)計(jì)器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
- //
- }
- [STAThread]
- static void Main()
- {
- System.Windows.Forms.Application.Run(new ItemToDoc());
- }
- ///
- /// 清理所有正在使用的資源。
- ///
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows Form Designer generated code
- ///
- /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
- /// 此方法的內(nèi)容。
- ///
- private void InitializeComponent()
- {
- //
- // ItemToDoc
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(292, 273);
- this.Name = "ItemToDoc";
- this.Text = "ItemToDoc";
- this.Load += new System.EventHandler(this.ItemToDoc_Load);
- }
- #endregion
- private void ItemToDoc_Load(object sender, System.EventArgs e)
- {
- WriteFile();
- }
- private void WriteFile()
- {
- strFileName=System.Windows.Forms.Application.StartupPath+"\\試題庫【"+GetRandomString()+"】.doc";
- Object Nothing=System.Reflection.Missing.Value;
- myWordDoc=myWordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
- #region 將數(shù)據(jù)庫中讀取得數(shù)據(jù)寫入到word文件中
- strContent="試題庫\n\n\r";
- WriteFile(strContent);
- strContent="試題庫";
- WriteFile(strContent);
- #endregion
- //將WordDoc文檔對(duì)象的內(nèi)容保存為DOC文檔
- myWordDoc.SaveAs(ref strFileName,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
- //關(guān)閉WordDoc文檔對(duì)象
- myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
- //關(guān)閉WordApp組件對(duì)象
- myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
- }
- ///
- /// 獲取一個(gè)隨即字符串
- ///
- ///
- private string GetRandomString()
- {
- DateTime iNow=DateTime.Now;
- string strDate=iNow.ToString("yyyyMMddHHmmffff");
- Random ran=new Random();
- int iRan=Convert.ToInt32(10000*ran.NextDouble());
- string strRan=iRan.ToString();
- //位數(shù)不足則補(bǔ)0
- int iRanlen=strRan.Length;
- for(int i=0;i<4-iRanlen;i++)
- {
- strRan="0"+strRan;
- }
- return strDate+strRan;
- }
- ///
- /// 將字符串寫入到Word文件中
- ///
- /// 要寫入的字符串
- private void WriteFile(string str)
- {
- myWordDoc.Paragraphs.Last.Range.Text=str;
- }
- }
- }
以上就是.NET操作Word的實(shí)現(xiàn)代碼。
【編輯推薦】