.NET操作Word的实现:using Word
创始人
2024-05-04 18:00:32
0

.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的实现代码。

【编辑推荐】

  1. ASP.NET新手问题总结
  2. 深入研究Repeater控件:最大的灵活性
  3. DataList控件入门介绍
  4. DataGrid Web控件运作机制探秘
  5. 小议ASP.NET数据Web控件之间的相似性

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...