国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

在.net中操作Word

2024-07-10 13:11:58
字體:
來源:轉載
供稿:網友
國內最大的酷站演示中心!
  要操作word,我們就需要word的對象庫文件“msword.olb”(word 2000為msword9.olb),通常安裝了office word后,你就可以在office安裝目錄的office10文件夾下面找到這個文件,當我們將這個文件引入到項目后,我們就可以在源碼中使用各種操作函數來操作word。具體做法是打開菜單欄中的項目>添加引用>瀏覽,在打開的“選擇組件”對話框中找到msword.olb后按確定即可引入此對象庫文件,vs.net將會自動將 庫文件轉化為dll組件,這樣我們只要在源碼中創建該組件對象即可達到操作word的目的!

在cs代碼文件中對命名空間的應用,如:using 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+"http://試題庫【"+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;
    }
   }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 观塘区| 双牌县| 汶川县| 濮阳市| 临湘市| 长春市| 双桥区| 河曲县| 偃师市| 双牌县| 华安县| 灵宝市| 长子县| 隆尧县| 额尔古纳市| 清镇市| 兴化市| 莫力| 应城市| 大庆市| 沙湾县| 平舆县| 青海省| 内丘县| 金秀| 阿克陶县| 苍梧县| 花垣县| 宜宾市| 平泉县| 马关县| 略阳县| 榆中县| 清水县| 金昌市| 卢龙县| 乌鲁木齐市| 临安市| 平邑县| 土默特左旗| 旌德县|