在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+"//试题库【"+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; 
    } 
   } 
  }
新闻热点
疑难解答
图片精选