C#实现合并多个word文档的方法
本文实例讲述了C#实现合并多个word文档的方法,是非常具有实用价值的技巧。分享给大家供大家参考。
具体实现方法如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Web; usingMicrosoft.Office.Interop.Word; usingSystem.Reflection; usingSystem.IO; usingSystem.Diagnostics; namespacedriverexam.WordReport { publicclassWordDocumentMerger { privateApplicationClassobjApp=null; privateDocumentobjDocLast=null; privateDocumentobjDocBeforeLast=null; publicWordDocumentMerger() { objApp=newApplicationClass(); } #region打开文件 privatevoidOpen(stringtempDoc) { objectobjTempDoc=tempDoc; objectobjMissing=System.Reflection.Missing.Value; objDocLast=objApp.Documents.Open( refobjTempDoc,//FileName refobjMissing,//ConfirmVersions refobjMissing,//ReadOnly refobjMissing,//AddToRecentFiles refobjMissing,//PasswordDocument refobjMissing,//PasswordTemplate refobjMissing,//Revert refobjMissing,//WritePasswordDocument refobjMissing,//WritePasswordTemplate refobjMissing,//Format refobjMissing,//Enconding refobjMissing,//Visible refobjMissing,//OpenAndRepair refobjMissing,//DocumentDirection refobjMissing,//NoEncodingDialog refobjMissing//XMLTransform ); objDocLast.Activate(); } #endregion #region保存文件到输出模板 privatevoidSaveAs(stringoutDoc) { objectobjMissing=System.Reflection.Missing.Value; objectobjOutDoc=outDoc; objDocLast.SaveAs( refobjOutDoc,//FileName refobjMissing,//FileFormat refobjMissing,//LockComments refobjMissing,//PassWord refobjMissing,//AddToRecentFiles refobjMissing,//WritePassword refobjMissing,//ReadOnlyRecommended refobjMissing,//EmbedTrueTypeFonts refobjMissing,//SaveNativePictureFormat refobjMissing,//SaveFormsData refobjMissing,//SaveAsAOCELetter, refobjMissing,//Encoding refobjMissing,//InsertLineBreaks refobjMissing,//AllowSubstitutions refobjMissing,//LineEnding refobjMissing//AddBiDiMarks ); } #endregion #region循环合并多个文件(复制合并重复的文件) ///<summary> ///循环合并多个文件(复制合并重复的文件) ///</summary> ///<paramname="tempDoc">模板文件</param> ///<paramname="arrCopies">需要合并的文件</param> ///<paramname="outDoc">合并后的输出文件</param> publicvoidCopyMerge(stringtempDoc,string[]arrCopies,stringoutDoc) { objectobjMissing=Missing.Value; objectobjFalse=false; objectobjTarget=WdMergeTarget.wdMergeTargetSelected; objectobjUseFormatFrom=WdUseFormattingFrom.wdFormattingFromSelected; try { //打开模板文件 Open(tempDoc); foreach(stringstrCopyinarrCopies) { objDocLast.Merge( strCopy,//FileName refobjTarget,//MergeTarget refobjMissing,//DetectFormatChanges refobjUseFormatFrom,//UseFormattingFrom refobjMissing//AddToRecentFiles ); objDocBeforeLast=objDocLast; objDocLast=objApp.ActiveDocument; if(objDocBeforeLast!=null) { objDocBeforeLast.Close( refobjFalse,//SaveChanges refobjMissing,//OriginalFormat refobjMissing//RouteDocument ); } } //保存到输出文件 SaveAs(outDoc); foreach(DocumentobjDocumentinobjApp.Documents) { objDocument.Close( refobjFalse,//SaveChanges refobjMissing,//OriginalFormat refobjMissing//RouteDocument ); } } finally { objApp.Quit( refobjMissing,//SaveChanges refobjMissing,//OriginalFormat refobjMissing//RoutDocument ); objApp=null; } } ///<summary> ///循环合并多个文件(复制合并重复的文件) ///</summary> ///<paramname="tempDoc">模板文件</param> ///<paramname="arrCopies">需要合并的文件</param> ///<paramname="outDoc">合并后的输出文件</param> publicvoidCopyMerge(stringtempDoc,stringstrCopyFolder,stringoutDoc) { string[]arrFiles=Directory.GetFiles(strCopyFolder); CopyMerge(tempDoc,arrFiles,outDoc); } #endregion #region循环合并多个文件(插入合并文件) ///<summary> ///循环合并多个文件(插入合并文件) ///</summary> ///<paramname="tempDoc">模板文件</param> ///<paramname="arrCopies">需要合并的文件</param> ///<paramname="outDoc">合并后的输出文件</param> publicvoidInsertMerge(stringtempDoc,string[]arrCopies,stringoutDoc) { objectobjMissing=Missing.Value; objectobjFalse=false; objectconfirmConversion=false; objectlink=false; objectattachment=false; try { //打开模板文件 Open(tempDoc); foreach(stringstrCopyinarrCopies) { objApp.Selection.InsertFile( strCopy, refobjMissing, refconfirmConversion, reflink, refattachment ); } //保存到输出文件 SaveAs(outDoc); foreach(DocumentobjDocumentinobjApp.Documents) { objDocument.Close( refobjFalse,//SaveChanges refobjMissing,//OriginalFormat refobjMissing//RouteDocument ); } } finally { objApp.Quit( refobjMissing,//SaveChanges refobjMissing,//OriginalFormat refobjMissing//RoutDocument ); objApp=null; } } ///<summary> ///循环合并多个文件(插入合并文件) ///</summary> ///<paramname="tempDoc">模板文件</param> ///<paramname="arrCopies">需要合并的文件</param> ///<paramname="outDoc">合并后的输出文件</param> publicvoidInsertMerge(stringtempDoc,stringstrCopyFolder,stringoutDoc) { string[]arrFiles=Directory.GetFiles(strCopyFolder); InsertMerge(tempDoc,arrFiles,outDoc); } #endregion } }
相信本文所述对大家的C#程序设计有一定的借鉴价值。