手頭上的一個項目報表相對比較簡單,所以報表打印采用vba引擎,通過定制word模版,然后根據模版需要填充數據,然后ok,打印即可。
實現方法:首先需要引用vba組建,我用的是office2003 professional,dll版本號為microsoft word11.0,
另外當然還需要引用interop.word.dll.
代碼如下:
#region 打開word文檔,并且返回對象wdoc,wdoc
  /// <summary>
  /// 打開word文檔,并且返回對象wdoc,wdoc
  /// </summary>
  /// <param name="filename">完整word文件路徑+名稱</param>  
  /// <param name="wdoc">返回的word.document wdoc對象</param>
  /// <param name="wapp">返回的word.application對象</param>
  public static void createworddocument(string filename,ref word.document wdoc,ref  word.application wapp)
  {
   if(filename == "") return;
   word.document thisdocument = null;
   word.formfields   formfields = null;
   word.application thisapplication = new word.applicationclass();
   thisapplication.visible = true;
   thisapplication.caption = "";
   thisapplication.options.checkspellingasyoutype = false;
   thisapplication.options.checkgrammarasyoutype = false;
   object filename = filename;
   object confirmconversions = false;
   object readonly = true;
   object addtorecentfiles = false;
   object passworddocument = system.type.missing;
   object passwordtemplate = system.type.missing;
   object revert = system.type.missing;
   object writepassworddocument = system.type.missing;
   object writepasswordtemplate = system.type.missing;
   object format = system.type.missing;
   object encoding = system.type.missing;
   object visible = system.type.missing;
   object openandrepair = system.type.missing;
   object documentdirection =  system.type.missing;
   object noencodingdialog = system.type.missing;
   object xmltransform = system.type.missing;
   try
   {
    word.document worddoc =
     thisapplication.documents.open(ref filename, ref confirmconversions,
     ref readonly, ref addtorecentfiles, ref passworddocument, ref passwordtemplate,
     ref revert,ref writepassworddocument, ref writepasswordtemplate, ref format,
     ref encoding, ref visible, ref openandrepair, ref documentdirection,
     ref noencodingdialog, ref xmltransform );
    
    thisdocument = worddoc;
    wdoc = worddoc;
    wapp = thisapplication;
    formfields = worddoc.formfields;
   }
   catch(exception ex)
   {
    messagebox.show(ex.message);
   }
   
  }
  #endregion
調用上面靜態方法,打開目標文件并且把datagrid中數據填充到對應word標簽中去
#region word填充數據(for example)
  /// <summary>
  /// word填充數據
  /// </summary>
  private void wordloaddata()
  {
   word.document wdoc=null;
   word.application wapp=null;
   sysfun.createworddocument("e://監測報告(new).dot",ref wdoc,ref wapp);
   //對標簽"c"進行填充
   object bkmc="c";
   if(wapp.activedocument.bookmarks.exists("c") == true)
   {
    wapp.activedocument.bookmarks.get_item
     (ref bkmc).select();
   }
   wapp.selection.typetext(this.txt1.text);
   object bkmg = "twatertable3";
   object unit; 
   object count; //移動數
   object extend; 
    
   extend = word.wdmovementtype.wdextend;
   unit = word.wdunits.wdcell;
   //把datagrid中數據填充到標簽twatertable3上
   if(wapp.activedocument.bookmarks.exists("twatertable3") == true)
   {
    wapp.activedocument.bookmarks.get_item
     (ref bkmg).select();
    for(int i=0;i<this.gridex1.recordcount;i++)
    {
     if(i==0)
     {
      count=1;
     }
     else
     {
      count=0;
     }
     //需填充5列數據
     wapp.selection.move(ref unit,ref count);
     wapp.selection.typetext(gridex1.getrow(i).cells[0].text);
     count=1;
      
     wapp.selection.move(ref unit,ref count);
     wapp.selection.typetext(gridex1.getrow(i).cells[1].text);
      
     wapp.selection.move(ref unit,ref count);
     wapp.selection.typetext(gridex1.getrow(i).cells[2].text);
      
     wapp.selection.move(ref unit,ref count);
     wapp.selection.typetext(gridex1.getrow(i).cells[3].text);
     
     wapp.selection.move(ref unit,ref count);
     wapp.selection.typetext(gridex1.getrow(i).cells[4].text);
     //換行
     wapp.selection.moveright(ref unit,ref count,ref extend);
    }
   }
  }
  #endregion
然后就ok了,在對標簽表控制要注意列循環和換行,不知道還有沒有其它好辦法,歡迎探討!
新聞熱點
疑難解答