DocMerger.cs
上传用户:tiancihang
上传日期:2014-03-12
资源大小:21387k
文件大小:10k
- using System;
- using System.Reflection;
- using System.IO;
- using System.Diagnostics;
- using Word;
- using Microsoft.Office.Core;
- namespace DocMergerComponent
- {
- /// <summary>
- /// Simple class to get a merged document based on a baseline document
- /// and a set of copies
- /// </summary>
- public class DocMerger
- {
- /// <summary>
- /// Merge a document with a set of copies
- /// </summary>
- /// <param name="strOrgDoc">
- /// Original file name
- /// </param>
- /// <param name="arrCopies">
- /// File names of the modified files
- /// </param>
- /// <param name="strOutDoc">
- /// The result filename
- /// </param>
- public void Merge(string strOrgDoc, string[] arrCopies, string strOutDoc)
- {
- ApplicationClass objApp = null;
- //boxing of default values for COM interop purposes
- object objMissing = Missing.Value;
- object objFalse = false;
- object objTarget = WdMergeTarget.wdMergeTargetSelected;
- object objUseFormatFrom = WdUseFormattingFrom.wdFormattingFromSelected;
- try
- {
- objApp = new ApplicationClass();
- object objOrgDoc = strOrgDoc;
-
- Document objDocLast = null;
- Document objDocBeforeLast = null;
- objDocLast = objApp.Documents.Open(
- ref objOrgDoc, //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
- );
- foreach(string strCopy in arrCopies)
- {
- Debug.WriteLine("Merging file " + strCopy);
- objDocLast.Merge(
- strCopy, //FileName
- ref objTarget, //MergeTarget
- ref objMissing, //DetectFormatChanges
- ref objUseFormatFrom, //UseFormattingFrom
- ref objMissing //AddToRecentFiles
- );
- objDocBeforeLast = objDocLast;
- objDocLast = objApp.ActiveDocument;
- Debug.WriteLine("The active document is " + objDocLast.Name);
- if (objDocBeforeLast != null)
- {
- Debug.WriteLine("Closing " + objDocBeforeLast.Name);
- objDocBeforeLast.Close(
- ref objFalse, //SaveChanges
- ref objMissing, //OriginalFormat
- ref objMissing //RouteDocument
- );
- }
-
-
- }
- object objOutDoc = strOutDoc;
-
- 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
- );
- 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>
- /// Merge a document with a set of copies
- /// </summary>
- /// <param name="strOrgDoc">
- /// Original file name
- /// </param>
- /// <param name="strCopyFolder">
- /// Folder in which the copies are located
- /// </param>
- /// <param name="strOutDoc">
- /// The result filename
- /// </param>
- public void Merge(string strOrgDoc, string strCopyFolder, string strOutDoc)
- {
- string[] arrFiles = Directory.GetFiles(strCopyFolder);
- Merge(strOrgDoc, arrFiles, strOutDoc);
- }
- }
- /// <summary>
- /// Word文档合并
- /// </summary>
- /*==============================================================================================
- *
- * stephen·周整理程序
- *
- *=============================================================================================*/
- public class MyDocMerger
- {
- private ApplicationClass oWordApplicatoin = null;
- private Document oDoc;
- /*==============================================================================================
- *
- * 合并单个文档
- *
- *=============================================================================================*/
- public void Merge(string strOrgDoc, string strCopyDoc, string strOutDoc)
- {
- //boxing of default values for COM interop purposes
- object objMissing = Missing.Value;
- object objFalse = false;
- try
- {
- object filename = strOrgDoc;
- object readOnly = false;
- object isVisible = true;
- object missing = System.Reflection.Missing.Value;
- oWordApplicatoin = new ApplicationClass();
- oDoc = oWordApplicatoin.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
- ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing, ref missing);
- oDoc.Activate();
- object confirmConversion = false;
- object link = false;
- object attachment = false;
- oWordApplicatoin.Selection.InsertFile(strCopyDoc, ref missing, ref confirmConversion, ref link, ref attachment);
- object filename_save = strOutDoc;
- oDoc.SaveAs(ref filename_save, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
- foreach (Document objDocument in oWordApplicatoin.Documents)
- {
- objDocument.Close(ref objFalse, ref objMissing, ref objMissing);
- }
- }
- finally
- {
- oWordApplicatoin.Quit(ref objMissing, ref objMissing, ref objMissing);
- }
- }
- /*==============================================================================================
- *
- * 合并多个文档
- *
- *=============================================================================================*/
- public void MergeDocArray(string strOrgDoc, string[] strCopiesDoc, string strOutDoc)
- {
- //boxing of default values for COM interop purposes
- object objMissing = Missing.Value;
- object objFalse = false;
- try
- {
- object filename = strOrgDoc;
- object readOnly = false;
- object isVisible = true;
- object missing = System.Reflection.Missing.Value;
- oWordApplicatoin = new ApplicationClass();
- oDoc = oWordApplicatoin.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
- ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing, ref missing);
- oDoc.Activate();
- foreach (string strCopy in strCopiesDoc)
- {
- object confirmConversion = false;
- object link = false;
- object attachment = false;
- oWordApplicatoin.Selection.InsertFile(strCopy, ref missing, ref confirmConversion, ref link, ref attachment);
- }
- object filename_save = strOutDoc;
- oDoc.SaveAs(ref filename_save, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
- foreach (Document objDocument in oWordApplicatoin.Documents)
- {
- objDocument.Close(ref objFalse, ref objMissing, ref objMissing);
- }
- }
- finally
- {
- oWordApplicatoin.Quit(ref objMissing, ref objMissing, ref objMissing);
- }
- }
- }
- }