浅析C#合并多个WORD文档的具体实现方法

今天因为客户需要,需要将多个WORD文档合并成为一个WORD文档。其中,对WORD文档的合并方式分两种形式:

一是复制合并;

一是插入合并,即将多个文档按照先后顺序合并到另一个文档中.

代码如下:

 
 
 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.Office.Interop.Word;
  5. using System.Reflection;
  6. using System.IO;
  7. using System.Diagnostics;
  8. namespace Eipsoft.Common
  9. {
  10.     /// 
  11.     /// Word文档合并类
  12.     /// 
  13.     public class WordDocumentMerger
  14.     {
  15.         private ApplicationClass objApp = null;
  16.         private Document objDocLast = null;
  17.         private Document objDocBeforeLast = null;
  18.         public WordDocumentMerger()
  19.         {
  20.             objApp = new ApplicationClass();
  21.         }
  22.         #region 打开文件
  23.         private void Open(string tempDoc)
  24.         {
  25.             object objTempDoc = tempDoc;
  26.             object objMissing = System.Reflection.Missing.Value;
  27.             objDocLast = objApp.Documents.Open(
  28.                  ref objTempDoc,    //FileName
  29.                  ref objMissing,   //ConfirmVersions
  30.                  ref objMissing,   //ReadOnly
  31.                  ref objMissing,   //AddToRecentFiles
  32.                  ref objMissing,   //PasswordDocument
  33.                  ref objMissing,   //PasswordTemplate
  34.                  ref objMissing,   //Revert
  35.                  ref objMissing,   //WritePasswordDocument
  36.                  ref objMissing,   //WritePasswordTemplate
  37.                  ref objMissing,   //Format
  38.                  ref objMissing,   //Enconding
  39.                  ref objMissing,   //Visible
  40.                  ref objMissing,   //OpenAndRepair
  41.                  ref objMissing,   //DocumentDirection
  42.                  ref objMissing,   //NoEncodingDialog
  43.                  ref objMissing    //XMLTransform
  44.                  );
  45.             objDocLast.Activate();
  46.         }
  47.         #endregion
  48.         #region 保存文件到输出模板
  49.         private void SaveAs(string outDoc)
  50.         {
  51.             object objMissing = System.Reflection.Missing.Value;
  52.             object objOutDoc = outDoc;
  53.             objDocLast.SaveAs(
  54.               ref objOutDoc,      //FileName
  55.               ref objMissing,     //FileFormat
  56.               ref objMissing,     //LockComments
  57.               ref objMissing,     //PassWord     
  58.               ref objMissing,     //AddToRecentFiles
  59.               ref objMissing,     //WritePassword
  60.               ref objMissing,     //ReadOnlyRecommended
  61.               ref objMissing,     //EmbedTrueTypeFonts
  62.               ref objMissing,     //SaveNativePictureFormat
  63.               ref objMissing,     //SaveFormsData
  64.               ref objMissing,     //SaveAsAOCELetter,
  65.               ref objMissing,     //Encoding
  66.               ref objMissing,     //InsertLineBreaks
  67.               ref objMissing,     //AllowSubstitutions
  68.               ref objMissing,     //LineEnding
  69.               ref objMissing      //AddBiDiMarks
  70.               );
  71.         }
  72.         #endregion
  73.         #region 循环合并多个文件(复制合并重复的文件)
  74.         /// 
  75.         /// 循环合并多个文件(复制合并重复的文件)
  76.         /// 
  77.         /// 模板文件
  78.         /// 需要合并的文件
  79.         /// 合并后的输出文件
  80.         public void CopyMerge(string tempDoc, string[] arrCopies, string outDoc)
  81.         {
  82.             object objMissing = Missing.Value;
  83.             object objFalse = false;
  84.             object objTarget = WdMergeTarget.wdMergeTargetSelected;
  85.             object objUseFormatFrom = WdUseFormattingFrom.wdFormattingFromSelected;
  86.             try
  87.             {
  88.                 //打开模板文件
  89.                 Open(tempDoc);
  90.                 foreach (string strCopy in arrCopies)
  91.                 {
  92.                     objDocLast.Merge(
  93.                       strCopy,                //FileName    
  94.                       ref objTarget,          //MergeTarget
  95.                       ref objMissing,         //DetectFormatChanges
  96.                       ref objUseFormatFrom,   //UseFormattingFrom
  97.                       ref objMissing          //AddToRecentFiles
  98.                       );
  99.                     objDocBeforeLast = objDocLast;
  100.                     objDocLast = objApp.ActiveDocument;
  101.                     if (objDocBeforeLast != null)
  102.                     {
  103.                         objDocBeforeLast.Close(
  104.                           ref objFalse,     //SaveChanges
  105.                           ref objMissing,   //OriginalFormat
  106.                           ref objMissing    //RouteDocument
  107.                           );
  108.                     }
  109.                 }
  110.                 //保存到输出文件
  111.                 SaveAs(outDoc);
  112.                 foreach (Document objDocument in objApp.Documents)
  113.                 {
  114.                     objDocument.Close(
  115.                       ref objFalse,     //SaveChanges
  116.                       ref objMissing,   //OriginalFormat
  117.                       ref objMissing    //RouteDocument
  118.                       );
  119.                 }
  120.             }
  121.             finally
  122.             {
  123.                 objApp.Quit(
  124.                   ref objMissing,     //SaveChanges
  125.                   ref objMissing,     //OriginalFormat
  126.                   ref objMissing      //RoutDocument
  127.                   );
  128.                 objApp = null;
  129.             }
  130.         }
  131.         /// 
  132.         /// 循环合并多个文件(复制合并重复的文件)
  133.         /// 
  134.         /// 模板文件
  135.         /// 需要合并的文件
  136.         /// 合并后的输出文件
  137.         public void CopyMerge(string tempDoc, string strCopyFolder, string outDoc)
  138.         {
  139.             string[] arrFiles = Directory.GetFiles(strCopyFolder);
  140.             CopyMerge(tempDoc, arrFiles, outDoc);
  141.         }
  142.         #endregion
  143.         #region 循环合并多个文件(插入合并文件)
  144.         /// 
  145.         /// 循环合并多个文件(插入合并文件)
  146.         /// 
  147.         /// 模板文件
  148.         /// 需要合并的文件
  149.         /// 合并后的输出文件
  150.         public void InsertMerge(string tempDoc, string[] arrCopies, string outDoc)
  151.         {
  152.             object objMissing = Missing.Value;
  153.             object objFalse = false;
  154.             object confirmConversion = false;
  155.             object link = false;
  156.             object attachment = false;
  157.             try
  158.             {
  159.                 //打开模板文件
  160.                 Open(tempDoc);
  161.                 foreach (string strCopy in arrCopies)
  162.                 {
  163.                     objApp.Selection.InsertFile(
  164.                         strCopy,
  165.                         ref objMissing,
  166.                         ref confirmConversion,
  167.                         ref link,
  168.                         ref attachment
  169.                         );
  170.                 }
  171.                 //保存到输出文件
  172.                 SaveAs(outDoc);
  173.                 foreach (Document objDocument in objApp.Documents)
  174.                 {
  175.                     objDocument.Close(
  176.                       ref objFalse,     //SaveChanges
  177.                       ref objMissing,   //OriginalFormat
  178.                       ref objMissing    //RouteDocument
  179.                       );
  180.                 }
  181.             }
  182.             finally
  183.             {
  184.                 objApp.Quit(
  185.                   ref objMissing,     //SaveChanges
  186.                   ref objMissing,     //OriginalFormat
  187.                   ref objMissing      //RoutDocument
  188.                   );
  189.                 objApp = null;
  190.             }
  191.         }
  192.         /// 
  193.         /// 循环合并多个文件(插入合并文件)
  194.         /// 
  195.         /// 模板文件
  196.         /// 需要合并的文件
  197.         /// 合并后的输出文件
  198.         public void InsertMerge(string tempDoc, string strCopyFolder, string outDoc)
  199.         {
  200.             string[] arrFiles = Directory.GetFiles(strCopyFolder);
  201.             InsertMerge(tempDoc, arrFiles, outDoc);
  202.         }
  203.         #endregion
  204.     }
  205. }

链接:http://www.cnblogs.com/madengwei/archive/2009/09/26/1574570.html

网站名称:浅析C#合并多个WORD文档的具体实现方法
地址分享:http://www.shufengxianlan.com/qtweb/news40/374040.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联