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

首頁 > 開發 > 綜合 > 正文

也說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收集整理,歡迎訪問。
  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 如东县| 罗甸县| 辽宁省| 晋宁县| 赞皇县| 五河县| 宕昌县| 通化县| 咸宁市| 金塔县| 崇仁县| 正蓝旗| 高要市| 朔州市| 谷城县| 民和| 开江县| 南澳县| 舟曲县| 阿拉善盟| 亚东县| 竹山县| 永宁县| 沈阳市| 张家界市| 威海市| 汽车| 滁州市| 兰坪| 新密市| 普宁市| 天等县| 同心县| 贵南县| 夏邑县| 分宜县| 岐山县| 兰考县| 恩平市| 遵义县| 金坛市|