欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现合并多个word文档的方法

程序员文章站 2023-12-16 22:55:16
本文实例讲述了c#实现合并多个word文档的方法,是非常具有实用价值的技巧。分享给大家供大家参考。 具体实现方法如下: using system; using...

本文实例讲述了c#实现合并多个word文档的方法,是非常具有实用价值的技巧。分享给大家供大家参考。

具体实现方法如下:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using microsoft.office.interop.word;
using system.reflection;
using system.io;
using system.diagnostics;

namespace driverexam.wordreport
{
  public class worddocumentmerger
  {
    private applicationclass objapp = null;
    private document objdoclast = null;
    private document objdocbeforelast = null;
    public worddocumentmerger()
    {
      objapp = new applicationclass();
    }
    #region 打开文件
    private void open(string tempdoc)
    {
      object objtempdoc = tempdoc;
      object objmissing = system.reflection.missing.value;

   objdoclast = objapp.documents.open(
      ref objtempdoc, //filename 
      ref objmissing, //confirmversions 
      ref objmissing, //readonly 
      ref objmissing, //addtorecentfiles 
      ref objmissing, //passworddocument 
      ref objmissing, //passwordtemplate 
      ref objmissing, //revert 
      ref objmissing, //writepassworddocument 
      ref objmissing, //writepasswordtemplate 
      ref objmissing, //format 
      ref objmissing, //enconding 
      ref objmissing, //visible 
      ref objmissing, //openandrepair 
      ref objmissing, //documentdirection 
      ref objmissing, //noencodingdialog 
      ref objmissing //xmltransform 
      );
      objdoclast.activate();
    }
    #endregion

    #region 保存文件到输出模板
    private void saveas(string outdoc)
    {
      object objmissing = system.reflection.missing.value;
      object objoutdoc = outdoc;
      objdoclast.saveas(
      ref objoutdoc, //filename 
      ref objmissing, //fileformat 
      ref objmissing, //lockcomments 
      ref objmissing, //password 
      ref objmissing, //addtorecentfiles 
      ref objmissing, //writepassword 
      ref objmissing, //readonlyrecommended 
      ref objmissing, //embedtruetypefonts 
      ref objmissing, //savenativepictureformat 
      ref objmissing, //saveformsdata 
      ref objmissing, //saveasaoceletter, 
      ref objmissing, //encoding 
      ref objmissing, //insertlinebreaks 
      ref objmissing, //allowsubstitutions 
      ref objmissing, //lineending 
      ref objmissing //addbidimarks 
      );
    }
    #endregion

    #region 循环合并多个文件(复制合并重复的文件)
    /// <summary> 
    /// 循环合并多个文件(复制合并重复的文件) 
    /// </summary> 
    /// <param name="tempdoc">模板文件</param> 
    /// <param name="arrcopies">需要合并的文件</param> 
    /// <param name="outdoc">合并后的输出文件</param> 
    public void copymerge(string tempdoc, string[] arrcopies, string outdoc)
    {
      object objmissing = missing.value;
      object objfalse = false;
      object objtarget = wdmergetarget.wdmergetargetselected;
      object objuseformatfrom = wduseformattingfrom.wdformattingfromselected;
      try
      {
        //打开模板文件 
        open(tempdoc);
        foreach (string strcopy in arrcopies)
        {
          objdoclast.merge(
          strcopy, //filename 
          ref objtarget, //mergetarget 
          ref objmissing, //detectformatchanges 
          ref objuseformatfrom, //useformattingfrom 
          ref objmissing //addtorecentfiles 
          );
          objdocbeforelast = objdoclast;
          objdoclast = objapp.activedocument;
          if (objdocbeforelast != null)
          {
            objdocbeforelast.close(
            ref objfalse, //savechanges 
            ref objmissing, //originalformat 
            ref objmissing //routedocument 
            );
          }
        }
        //保存到输出文件 
        saveas(outdoc);
        foreach (document objdocument in objapp.documents)
        {
          objdocument.close(
          ref objfalse, //savechanges 
          ref objmissing, //originalformat 
          ref objmissing //routedocument 
          );
        }
      }
      finally
      {
        objapp.quit(
        ref objmissing, //savechanges 
        ref objmissing, //originalformat 
        ref objmissing //routdocument 
        );
        objapp = null;
      }
    }
    /// <summary> 
    /// 循环合并多个文件(复制合并重复的文件) 
    /// </summary> 
    /// <param name="tempdoc">模板文件</param> 
    /// <param name="arrcopies">需要合并的文件</param> 
    /// <param name="outdoc">合并后的输出文件</param> 
    public void copymerge(string tempdoc, string strcopyfolder, string outdoc)
    {
      string[] arrfiles = directory.getfiles(strcopyfolder);
      copymerge(tempdoc, arrfiles, outdoc);
    }
    #endregion

    #region 循环合并多个文件(插入合并文件)
    /// <summary> 
    /// 循环合并多个文件(插入合并文件) 
    /// </summary> 
    /// <param name="tempdoc">模板文件</param> 
    /// <param name="arrcopies">需要合并的文件</param> 
    /// <param name="outdoc">合并后的输出文件</param> 
    public void insertmerge(string tempdoc, string[] arrcopies, string outdoc)
    {
      object objmissing = missing.value;
      object objfalse = false;
      object confirmconversion = false;
      object link = false;
      object attachment = false;
      try
      {
        //打开模板文件 
        open(tempdoc);
        foreach (string strcopy in arrcopies)
        {
          objapp.selection.insertfile(
          strcopy,
          ref objmissing,
          ref confirmconversion,
          ref link,
          ref attachment
          );
        }
        //保存到输出文件 
        saveas(outdoc);
        foreach (document objdocument in objapp.documents)
        {
          objdocument.close(
          ref objfalse, //savechanges 
          ref objmissing, //originalformat 
          ref objmissing //routedocument 
          );
        }
      }
      finally
      {
        objapp.quit(
        ref objmissing, //savechanges 
        ref objmissing, //originalformat 
        ref objmissing //routdocument 
        );
        objapp = null;
      }
    }
    /// <summary> 
    /// 循环合并多个文件(插入合并文件) 
    /// </summary> 
    /// <param name="tempdoc">模板文件</param> 
    /// <param name="arrcopies">需要合并的文件</param> 
    /// <param name="outdoc">合并后的输出文件</param> 
    public void insertmerge(string tempdoc, string strcopyfolder, string outdoc)
    {
      string[] arrfiles = directory.getfiles(strcopyfolder);
      insertmerge(tempdoc, arrfiles, outdoc);
    }
    #endregion
  }
}

相信本文所述对大家的c#程序设计有一定的借鉴价值。

上一篇:

下一篇: