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

代碼如下:using System;
using Sys " /> 日韩不卡一区,成人在线播放,成人在线看片

中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

用C#編程合并多個(gè)WORD文檔

今天因?yàn)榭蛻?hù)需要,需要將多個(gè)WORD文檔合并成為一個(gè)WORD文檔。其中,對(duì)WORD文檔的合并方式分兩種形式:
一是復(fù)制合并;
一是插入合并,即將多個(gè)文檔按照先后順序合并到另一個(gè)文檔中.

代碼如下:

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文檔合并類(lèi)
///
public class WordDocumentMerger
{
private ApplicationClass objApp = null;
private Document objDocLast = null;
private Document objDocBeforeLast = null;
public WordDocumentMerger()
{
objApp
= new ApplicationClass();
}
#region 打開(kāi)文件
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 循環(huán)合并多個(gè)文件(復(fù)制合并重復(fù)的文件)
///
/// 循環(huán)合并多個(gè)文件(復(fù)制合并重復(fù)的文件)
///
/// 模板文件
/// 需要合并的文件
/// 合并后的輸出文件
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
{
//打開(kāi)模板文件
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;
}
}
///
/// 循環(huán)合并多個(gè)文件(復(fù)制合并重復(fù)的文件)
///
/// 模板文件
/// 需要合并的文件
/// 合并后的輸出文件
public void CopyMerge(string tempDoc, string strCopyFolder, string outDoc)
{
string[] arrFiles = Directory.GetFiles(strCopyFolder);
CopyMerge(tempDoc, arrFiles, outDoc);
}
#endregion

#region 循環(huán)合并多個(gè)文件(插入合并文件)
///
/// 循環(huán)合并多個(gè)文件(插入合并文件)
///
/// 模板文件
/// 需要合并的文件
/// 合并后的輸出文件
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
{
//打開(kāi)模板文件
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;
}
}
///
/// 循環(huán)合并多個(gè)文件(插入合并文件)
///
/// 模板文件
/// 需要合并的文件
/// 合并后的輸出文件
public void InsertMerge(string tempDoc, string strCopyFolder, string outDoc)
{
string[] arrFiles = Directory.GetFiles(strCopyFolder);
InsertMerge(tempDoc, arrFiles, outDoc);
}
#endregion
}
}

NET技術(shù)用C#編程合并多個(gè)WORD文檔,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: www.亚洲一区二区三区 | a级大片免费观看 | 欧美一区二区二区 | 超碰在线播| 国产精品影视在线观看 | 综合色播| 噜啊噜在线 | 精品一区二区三区中文字幕 | 日韩国产高清在线观看 | 久久久久国产 | 欧美一级二级视频 | av在线一区二区三区 | 亚洲视频中文字幕 | 高清国产午夜精品久久久久久 | 成人精品福利 | 久久综合av | 人人玩人人添人人澡欧美 | 中文字幕精品一区二区三区在线 | 四虎成人在线播放 | 91一区二区三区 | 综合色在线 | 国产区免费视频 | 中文字幕视频在线免费 | 欧美一区免费 | 国产一区免费 | 91久久视频 | 91免费视频观看 | 午夜精品久久久久久不卡欧美一级 | 综合婷婷 | 成人精品国产 | 日韩av啪啪网站大全免费观看 | 精品亚洲一区二区三区 | 男女激情网站免费 | 免费在线观看一区二区三区 | 男女网站免费观看 | 久久久久久久久99精品 | 中文字幕日韩专区 | 亚洲免费福利视频 | 欧美一级毛片久久99精品蜜桃 | 久久久久久久久91 | 一本色道久久综合亚洲精品高清 |