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

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

原文标题:用C#编程合并多个WORD文档

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

【编辑推荐】

  1. C#参差数组初始化概述
  2. C#数组初始化全面分析
  3. C#一维数组和多维数组浅谈
  4. C#参差数组初始化概述
  5. C#动态数组实例介绍
责任编辑:彭凡 来源: 博客园
相关推荐

2009-08-28 17:34:14

读取word文档

2009-09-01 13:13:28

C#打开Word文档

2009-09-01 18:29:24

C#实现多个接口

2009-08-12 16:26:30

C#读取XML文档

2009-09-07 09:36:29

C# DisposeDispose方法

2009-09-04 13:55:04

C#文档自动化

2009-08-31 10:38:34

C#变量初始化

2009-09-01 13:25:25

C#Word文档替换

2009-08-19 10:25:14

C#操作Word

2009-08-31 13:53:03

C#创建一个文件

2009-08-19 09:42:52

C#操作Word书签

2009-09-11 09:59:47

2009-08-19 11:13:49

C#操作Word

2009-08-19 11:34:06

C#操作Word

2009-09-11 09:15:06

C# get方法

2009-09-10 14:52:55

C# get

2009-08-20 16:15:19

C# 匿名方法

2009-08-10 17:36:17

C#扩展方法

2009-08-17 16:59:47

C#转义字符双引号

2009-08-12 15:26:38

C#读取XML文档
点赞
收藏

51CTO技术栈公众号