首页 > 开发 > 综合 > 正文

也说C#实现对Word文件读写

2024-07-21 02:17:54
字体:
来源:转载
供稿:网友
 

手头上的一个项目报表相对比较简单,所以报表打印采用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了,在对标签表控制要注意列循环和换行,不知道还有没有其它好办法,欢迎探讨!

  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表