国产探花免费观看_亚洲丰满少妇自慰呻吟_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;
    }
   }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丰县| 涡阳县| 普宁市| 确山县| 昌黎县| 临漳县| 页游| 望江县| 城步| 吉木萨尔县| 葵青区| 满城县| 明水县| 东海县| 宝山区| 辽阳县| 乐都县| 聂荣县| 湖南省| 德安县| 曲周县| 丹江口市| 定远县| 岳池县| 卢湾区| 新余市| 新郑市| 嘉峪关市| 襄城县| 壤塘县| 仁怀市| 田东县| 合山市| 新巴尔虎左旗| 洛浦县| 新和县| 景德镇市| 塘沽区| 尉氏县| 靖安县| 尉氏县|