一是復制合并;
一是插入合并,即將多個文檔按照先后順序合并到另一個文檔中.

代碼如下:using System;
using Sys " /> 天天拍夜夜添久久精品免费,亚洲欧美日韩综合在线,国产一区二区三区免费在线视频

一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

用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
}
}

NET技術用C#編程合并多個WORD文檔,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 久久精品全国免费观看国产 | 99久久香蕉国产综合影院 | 欧美日韩一区二区亚洲 | 欧美一级高清片免费一级 | 国产欧美日韩综合精品无毒 | 国产精品视频一区二区噜噜 | 久草免费在线 | 看黄网站在线 | 麻豆国产| 岛国一区 | 久久久久久久久久福利 | 91精品久久久久 | 婷婷亚洲综合五月天小说在线 | 午夜国产大片免费观看 | 国产99久9在线视频 国产99久久精品 | 在线观看免费污视频 | 国产精品久久久久久吹潮 | 伊人网综合在线视频 | 麻豆国产精品视频 | 国产成人咱精品视频免费网站 | 好属妞这里只有精品久久 | 最新97超级碰碰碰碰久久久久 | 成年人激情网站 | 91精品国产麻豆91久久久久久 | 国产精品一区伦免视频播放 | 国产日韩精品欧美在线ccc | 最新毛片久热97免费精品视频 | 日韩一区二区久久久久久 | 国产人伦激情在线观看 | 人人干在线视频 | 亚洲卡5卡6卡7卡2021入口 | 亚洲专区一路线二 | 欧美zooz人禽交免费观看 | 免费精品一区二区三区在线观看 | 婷五月综合 | 青青热久久综合网伊人 | 69国产成人综合久久精品91 | 亚洲图片另类 | 免费精品美女久久久久久久久 | 婷婷影院在线综合免费视频 | 色网视频在线观看 |