成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

淺析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-08-19 10:25:14

C#操作Word

2009-09-01 13:25:25

C#Word文檔替換

2009-08-19 09:42:52

C#操作Word書簽

2009-08-31 13:53:03

C#創建一個文件

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-19 11:13:49

C#操作Word

2009-08-19 11:34:06

C#操作Word

2009-08-17 16:59:47

C#轉義字符雙引號

2009-08-12 15:26:38

C#讀取XML文檔

2009-09-11 09:59:47

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩中文字幕一区二区 | 麻豆一区二区三区 | 亚洲国产精品久久 | 黄色片视频网站 | 精品久久久久久久 | 欧美黄色精品 | 久久久久久久久久久久久久久久久久久久 | 国产成人99久久亚洲综合精品 | 日韩中文字幕高清 | 欧美精品1区2区 | 亚洲成人一区 | 国产精品污www在线观看 | 欧美日韩中文字幕在线 | 亚洲欧洲中文日韩 | 夜夜爽99久久国产综合精品女不卡 | 中文字幕一区二区三区日韩精品 | 久久国产精品亚洲 | 午夜爽爽爽男女免费观看影院 | 欧美精品在线一区 | 国产真实乱全部视频 | 91精品国产91久久久久久最新 | 欧美一区二区三区在线观看 | 欧美精品久久久久久 | 狠狠操狠狠搞 | 久久久久国产一区二区三区四区 | 色橹橹欧美在线观看视频高清 | 午夜影院免费体验区 | av网址在线 | 天天躁日日躁xxxxaaaa | 成人精品一区二区 | 国产精品成人69xxx免费视频 | 亚洲日本免费 | 91性高湖久久久久久久久_久久99 | 免费人成在线观看网站 | 国产99久久精品一区二区300 | 酒色成人网 | 九九热精品视频在线观看 | 黄色片a级 | 毛片a区| 欧美日韩在线免费观看 | 欧美一二三四成人免费视频 |