成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

.NET操作Word的實現:using Word

開發 后端
本文介紹.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范例如下:

  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控件之間的相似性
責任編輯:yangsai 來源: 中國.NET論壇
相關推薦

2010-01-21 14:49:44

VB.NET操作Wor

2009-08-19 10:16:15

C#操作Word

2009-07-24 10:23:07

WORD文件轉換PDFASP.NET

2009-08-19 10:42:08

C#操作Word表格

2025-01-15 13:46:23

2009-08-27 15:53:30

C#中using wo

2011-10-28 16:53:04

Jacob

2009-08-19 10:46:48

C#操作Word表格

2009-08-19 10:25:14

C#操作Word

2009-10-26 17:10:53

VB.NET word

2009-08-19 11:13:49

C#操作Word

2009-08-19 11:34:06

C#操作Word

2009-08-19 09:42:52

C#操作Word書簽

2009-09-01 13:25:25

C#Word文檔替換

2009-09-01 11:21:02

C#讀取word內容

2009-08-19 11:23:12

C#操作Word

2011-03-23 15:06:45

PBWORD

2009-10-20 16:17:37

VB.NET Word

2020-09-24 14:40:55

Python 開發編程語言

2011-06-21 17:09:31

打印機技巧
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久久久久久国产精品视频 | 欧洲精品久久久久毛片完整版 | 精品日韩一区二区 | 亚洲国产一 | 久久99视频这里只有精品 | 亚洲成人中文字幕 | 操射视频 | 91精品久久久久久久久中文字幕 | 国产成人精品午夜视频免费 | 一级毛片免费视频观看 | 日韩成人专区 | 久久九九色 | 国产精品欧美精品 | 日本精品一区二区三区视频 | 亚洲精品成人网 | 亚洲午夜精品一区二区三区他趣 | 欧美日韩在线视频一区 | 国产精品日韩欧美一区二区三区 | 国产精品久久久久久吹潮日韩动画 | 亚洲色图网址 | 精品久久久久久亚洲精品 | 91久久精品一区二区二区 | av天天看 | 亚洲97| 久久久女| 色综合久久天天综合网 | 成人免费三级电影 | 久久精品这里精品 | 色综久久| 欧美9999 | 日韩精品一区二区三区中文字幕 | 欧洲一级毛片 | 久久久精品一区二区三区 | 中文成人无字幕乱码精品 | 国产成人一区二区三区 | 日韩中文字幕一区二区 | 99福利在线观看 | 精品国产91亚洲一区二区三区www | 成人精品久久 | 激情一区二区三区 | 精品欧美一区二区三区久久久 |