.NET操作Word的實現:using Word
.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范例如下:
- 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 窗體設計器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 調用后添加任何構造函數代碼
- //
- }
- [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
- ///
- /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
- /// 此方法的內容。
- ///
- 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 將數據庫中讀取得數據寫入到word文件中
- strContent="試題庫\n\n\r";
- WriteFile(strContent);
- strContent="試題庫";
- WriteFile(strContent);
- #endregion
- //將WordDoc文檔對象的內容保存為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);
- //關閉WordDoc文檔對象
- myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
- //關閉WordApp組件對象
- myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
- }
- ///
- /// 獲取一個隨即字符串
- ///
- ///
- 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();
- //位數不足則補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的實現代碼。
【編輯推薦】