.NET操作Word的实现:using Word

来源:互联网 发布:网络乌拉是什么意思 编辑:程序博客网 时间:2024/04/29 01:36
摘要: .NET操作Word可以用using Word来实现。基本上,vs.net将会自动将 库文件转化为DLL组件,这样我们只要在源码中创建该组件对象即可达到操作Word的目的。要实现,我们就需要Word的对象库文件“MSWORD.OLB”(word 2000为 ...

.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范例如下:

  1. using System;   
  2. using System.Drawing;   
  3. using System.Collections;   
  4. using System.ComponentModel;   
  5. using System.Windows.Forms;   
  6. using Word;   
  7.  
  8. namespace ExamSecure   
  9. {   
  10.  ///    
  11.  /// ItemToDoc 的摘要说明。   
  12.  ///    
  13.  public class ItemToDoc : System.Windows.Forms.Form   
  14.  {   
  15.   object strFileName;   
  16.   Object Nothing;   
  17.   Word.ApplicationClass myWordApp=new Word.ApplicationClass();   
  18.   Word.Document myWordDoc;   
  19.   string strContent="";   
  20.  
  21.   private System.ComponentModel.Container components = null;   
  22.  
  23.   public ItemToDoc()   
  24.   {   
  25.    //   
  26.    // Windows 窗体设计器支持所必需的   
  27.    //   
  28.    InitializeComponent();   
  29.  
  30.    //   
  31.    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码   
  32.    //   
  33.   }   
  34.   [STAThread]   
  35.   static void Main()    
  36.   {   
  37.    System.Windows.Forms.Application.Run(new ItemToDoc());   
  38.   }   
  39.   ///    
  40.   /// 清理所有正在使用的资源。   
  41.   ///    
  42.   protected override void Dispose( bool disposing )   
  43.   {   
  44.    if( disposing )   
  45.    {   
  46.     if(components != null)   
  47.     {   
  48.      components.Dispose();   
  49.     }   
  50.    }   
  51.    base.Dispose( disposing );   
  52.   }   
  53.  
  54.   #region Windows Form Designer generated code   
  55.   ///    
  56.   /// 设计器支持所需的方法 - 不要使用代码编辑器修改   
  57.   /// 此方法的内容。   
  58.   ///    
  59.   private void InitializeComponent()   
  60.   {   
  61.    //    
  62.    // ItemToDoc   
  63.    //    
  64.    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);   
  65.    this.ClientSize = new System.Drawing.Size(292, 273);   
  66.    this.Name = "ItemToDoc";   
  67.    this.Text = "ItemToDoc";   
  68.    this.Load += new System.EventHandler(this.ItemToDoc_Load);   
  69.  
  70.   }   
  71.   #endregion   
  72.  
  73.   private void ItemToDoc_Load(object sender, System.EventArgs e)   
  74.   {   
  75.    WriteFile();   
  76.   }   
  77.   private void WriteFile()   
  78.   {   
  79.      
  80.    strFileName=System.Windows.Forms.Application.StartupPath+"\\试题库【"+GetRandomString()+"】.doc";   
  81.    Object Nothing=System.Reflection.Missing.Value;   
  82.    myWordDoc=myWordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);   
  83.       
  84.    #region 将数据库中读取得数据写入到word文件中   
  85.  
  86.    strContent="试题库\n\n\r";   
  87.    WriteFile(strContent);   
  88.       
  89.    strContent="试题库";   
  90.    WriteFile(strContent);   
  91.  
  92.  
  93.    #endregion    
  94.       
  95.    //将WordDoc文档对象的内容保存为DOC文档   
  96.    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);   
  97.    //关闭WordDoc文档对象   
  98.    myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);   
  99.    //关闭WordApp组件对象   
  100.    myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);   
  101.   }   
  102.  
  103.   ///    
  104.   /// 获取一个随即字符串   
  105.   ///    
  106.   ///    
  107.   private string GetRandomString()   
  108.   {   
  109.    DateTime iNow=DateTime.Now;   
  110.    string strDate=iNow.ToString("yyyyMMddHHmmffff");   
  111.       
  112.    Random ran=new Random();   
  113.    int iRan=Convert.ToInt32(10000*ran.NextDouble());   
  114.    string strRan=iRan.ToString();   
  115.    //位数不足则补0      
  116.    int iRanlen=strRan.Length;   
  117.    for(int i=0;i<4-iRanlen;i++)   
  118.    {   
  119.     strRan="0"+strRan;   
  120.    }   
  121.    return strDate+strRan;   
  122.   }   
  123.  
  124.  
  125.   ///    
  126.   /// 将字符串写入到Word文件中   
  127.   ///    
  128.   /// 要写入的字符串   
  129.   private void WriteFile(string str)   
  130.   {   
  131.    myWordDoc.Paragraphs.Last.Range.Text=str;   
  132.   }   
  133.  
  134.  
  135.  }   
  136. }   

以上就是.NET操作Word的实现代码。