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

.NET操作Word的實(shí)現(xiàn):using Word

開(kāi)發(fā) 后端
本文介紹.NET操作Word的實(shí)現(xiàn)方法。通過(guò)Word的對(duì)象庫(kù)文件“MSWORD.OLB”(word 2000為MSWORD9.OLB),就可以實(shí)現(xiàn)這個(gè)功能。

.NET操作Word可以用using Word來(lái)實(shí)現(xiàn)。基本上,vs.net將會(huì)自動(dòng)將 庫(kù)文件轉(zhuǎn)化為DLL組件,這樣我們只要在源碼中創(chuàng)建該組件對(duì)象即可達(dá)到操作Word的目的。

要實(shí)現(xiàn),我們就需要Word的對(duì)象庫(kù)文件“MSWORD.OLB”(word 2000為MSWORD9.OLB),通常安裝了Office Word后,你就可以在office安裝目錄的Office10文件夾下面找到這個(gè)文件,當(dāng)我們將這個(gè)文件引入到項(xiàng)目后,我們就可以在源碼中使用各種操作函數(shù)來(lái)操作Word。具體做法是打開(kāi)菜單欄中的項(xiàng)目>添加引用>瀏覽,在打開(kāi)的“選擇組件”對(duì)話框中找到MSWORD.OLB后按確定即可引入此對(duì)象庫(kù)文件。

在CS代碼文件中對(duì)命名空間的應(yīng)用,如: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 的摘要說(shuō)明。   
  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 窗體設(shè)計(jì)器支持所必需的   
  27.    //   
  28.    InitializeComponent();   
  29.  
  30.    //   
  31.    // TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼   
  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.   /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改   
  57.   /// 此方法的內(nèi)容。   
  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+"\\試題庫(kù)【"+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 將數(shù)據(jù)庫(kù)中讀取得數(shù)據(jù)寫入到word文件中   
  85.  
  86.    strContent="試題庫(kù)\n\n\r";   
  87.    WriteFile(strContent);   
  88.       
  89.    strContent="試題庫(kù)";   
  90.    WriteFile(strContent);   
  91.  
  92.  
  93.    #endregion    
  94.       
  95.    //將WordDoc文檔對(duì)象的內(nèi)容保存為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.    //關(guān)閉WordDoc文檔對(duì)象   
  98.    myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);   
  99.    //關(guān)閉WordApp組件對(duì)象   
  100.    myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);   
  101.   }   
  102.  
  103.   ///    
  104.   /// 獲取一個(gè)隨即字符串   
  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.    //位數(shù)不足則補(bǔ)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的實(shí)現(xiàn)代碼。

【編輯推薦】

  1. ASP.NET新手問(wèn)題總結(jié)
  2. 深入研究Repeater控件:最大的靈活性
  3. DataList控件入門介紹
  4. DataGrid Web控件運(yùn)作機(jī)制探秘
  5. 小議ASP.NET數(shù)據(jù)Web控件之間的相似性
責(zé)任編輯:yangsai 來(lái)源: 中國(guó).NET論壇
相關(guān)推薦

2010-01-21 14:49:44

VB.NET操作Wor

2009-08-19 10:16:15

C#操作Word

2009-07-24 10:23:07

WORD文件轉(zhuǎn)換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內(nèi)容

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 開(kāi)發(fā)編程語(yǔ)言

2011-06-21 17:09:31

打印機(jī)技巧
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 久久精品国产a三级三级三级 | 一区 | 91精品国产色综合久久不卡98口 | 亚洲精品国产电影 | 亚洲精品大片 | 日韩国产高清在线观看 | 伊人久久大香线 | 91精品国产91久久综合桃花 | 亚洲精品区 | 久久精品免费 | 亚洲成av人片在线观看 | 日韩视频高清 | 日韩在线不卡 | 欧美日韩黄 | 欧美在线色| 亚洲视频一区二区三区四区 | 日韩中文字幕在线视频 | 欧美一级欧美一级在线播放 | 中文字幕免费 | 喷水毛片 | 国产精品黄视频 | 亚洲一区视频在线 | 欧美精品在线播放 | 精品一区二区三区四区 | 在线播放中文字幕 | 成人免费视频网站在线看 | 亚洲最大福利网 | 亚洲精品一区二区三区在线 | 日韩久久久久 | 亚洲一区二区精品视频在线观看 | 91成人免费看片 | 91看片官网 | 日韩在线播放第一页 | 在线观看视频亚洲 | 国产精品久久久爽爽爽麻豆色哟哟 | 精品久久久久一区二区国产 | 韩日在线 | 五月香婷婷 | 成人av电影天堂 | 亚洲欧美日韩精品久久亚洲区 | 视频在线一区二区 |