艾伟_转载:用C#编程合并多个WORD文档

今天因为客户需要,需要将多个WORD文档合并成为一个WORD文档。其中,对WORD文档的合并方式分两种形式:
一是复制合并;
一是插入合并,即将多个文档按照先后顺序合并到另一个文档中.

代码如下:

using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word;using System.Reflection;using System.IO;using System.Diagnostics;namespace Eipsoft.Common{////// Word文档合并类///    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 循环合并多个文件(复制合并重复的文件)////// 循环合并多个文件(复制合并重复的文件)////// 模板文件/// 需要合并的文件/// 合并后的输出文件        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;            }        }////// 循环合并多个文件(复制合并重复的文件)////// 模板文件/// 需要合并的文件/// 合并后的输出文件        public void CopyMerge(string tempDoc, string strCopyFolder, string outDoc)        {string[] arrFiles = Directory.GetFiles(strCopyFolder);            CopyMerge(tempDoc, arrFiles, outDoc);        }#endregion

#region 循环合并多个文件(插入合并文件)////// 循环合并多个文件(插入合并文件)////// 模板文件/// 需要合并的文件/// 合并后的输出文件        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;            }        }////// 循环合并多个文件(插入合并文件)////// 模板文件/// 需要合并的文件/// 合并后的输出文件        public void InsertMerge(string tempDoc, string strCopyFolder, string outDoc)        {string[] arrFiles = Directory.GetFiles(strCopyFolder);            InsertMerge(tempDoc, arrFiles, outDoc);        }#endregion    }}
时间: 2024-08-31 04:19:44

艾伟_转载:用C#编程合并多个WORD文档的相关文章

用C#编程合并多个WORD文档

今天因为客户需要,需要将多个WORD文档合并成为一个WORD文档.其中,对WORD文档的合并方式分两种形式:一是复制合并;一是插入合并,即将多个文档按照先后顺序合并到另一个文档中. 代码如下: using System; using System. Collections.Generic; using System.Text; using Microsoft.Office.Interop.Word; using System.Reflection; using System.IO; using

艾伟_转载:C#来创建和读取XML文档

扩展标记语言XML(eXtensible Markup Language),是由W3C组织制定的.做为用于替代HTML语言的一种新型的标记语言,XML内部有着很多基本标准,XML就是通过与这些相关标准地结合,应用于科学计算.电子出版.多媒体制作和电子商务的.C#作为一种新型的程序语言,是.Net框架的一个重要组成部分,他和XML的关系颇深.本文就从一个方面来探讨一下这二者的关系.即:看用C#如何来创建和读取XML文档. 一.本文程序设计和运行的软件环境 (一)微软公司视窗2000服务器 (二).

C#编程实现动态生成Word文档

如何用C#编程实现动态生成Word文档并填充数据的效果呢?要使用C#操作word,首先要添加引用     1.添加引用->COM->Microsoft Word 11.0 Object Library     2.在.cs文件中添加     using  Word;     下面的例子中包括C#对Word文档的创建.插入表格.设置样式等操作:     (例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法)     public   string  CreateWor

C#怎么合并多个word文档

问题描述 文档是通过读取模版用代码赋值后生成的新word,怎么将新生成的几个word合并成一个,然后存为PDF格式呢 解决方案 解决方案二:我以前做过类似的项目但是我们做的是将word转成一个个pdf然后合并pdf的不知道你需要吗?需要的话我帮你找找源码解决方案三:引用1楼u012394290的回复: 我以前做过类似的项目但是我们做的是将word转成一个个pdf然后合并pdf的不知道你需要吗?需要的话我帮你找找源码 这个应该是更好的思路.如果不是代码实现的话,都是使用AdobeAcrobatX进

怎么合并多个word文档?word文档合并方法

1.如下图所示我们随便找两个文档来进行合并测试吧. 2.先看看文档里面的内容,下面会有详细说明. 3.打开一个文档我们先定位光标的位置,这里要用到插入我们一般是放光标放到文档头或尾部,当然放中间也是可以的,个人工作需要自己定吧,我这里测试就放这个位置了. 4.光标定位好了我们点击"菜单栏"-"插入"-"文件",如下图所示. 5.在这里我们只选择WORD文档了,其它的不管了. 6.然后在这里找到文档之后再点击"插入" 7.最后我

合并Word文档的另类方法

今天遇到一个问题,就是需要把多个Word文档的内容追加到一个目标Word文档的后面,如果我有目标文档a.doc以及其他很多个文档b.doc,c.doc-等等数量很多.这个问题,如果是在服务端的话,直接使用OpenXML技术,读写文档就可以实现,这样性能较稳定,但是需要对OpenXML有一定的了解.如果在客户端机器上,可以使用Word PIA实现. 由于本人对于Word PIA较熟悉,所以采用了该方法.但是在实现的过程中,也是有很多种思路的. 将b.doc打开,将其中的内容选中,复制到剪贴板,然后

如何实现word文档的合并

问题描述 我有多个word文档,现在想把多个word文档的内容合并到一个word文档中,望大家帮帮忙!!!谢谢最好提供vb.net的代码! 解决方案 解决方案二:http://www.codeproject.com/csharp/mswmergecs.asp解决方案三:'files:Word文件'aimFileFullPath:要保存为的Word全路径(包括文件名,例如:D:a.doc)PrivateSubMergeWordDocuments(ByValfiles()AsString,ByVal

艾伟_转载:编写自文档化的代码

文所以载道也.  -- 宋·周敦颐<通书·文辞> 对于我们程序员来说,我们的工作也是写作--几乎每天都要写代码:而且还要载"道",不仅仅要满足客户的需求,还要让代码具有高度的可读性,这样其他的程序员可以更容易地对代码进行修改和扩展. 按这样的要求,我们需要为代码编写足够的文档,也就是将代码"文档化".常见的做法有两种,外部文档和注释. 外部文档 外部文档指的是在代码文件之外编写的附加文档,比如在Word文档中采用大量的篇幅(如UML图.表格)来设计或记录

从ASP.NET得到Microsoft Word文档的代码_实用技巧

背景 自动化(Automation)是一个过程,它允许编程语言譬如Visual Basic.NET或C#写的应用程序可以编程控制其它应用程序.自动化到Word允许你执行像创建新文档,向文档中添加文本,邮件合并,还有控制文档格式这样的操作.使用Word和其它Microsoft Office应用程序,几乎所有你能在用户面板上手动实现的操作都可以通过自动化编程实现.Word通过一个对象模型来实现这个编程功能性(programmatically functionality).对象模型是一系列类和方法,它