今天因為客戶需要
一是復制合並;
一是插入合並
代碼如下:
using System;
using System
using System
using Microsoft
using System
using System
using System
namespace Eipsoft
{
/// <summary>
/// Word文檔合並類
/// </summary>
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
objDocLast = objApp
ref objTempDoc
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing //XMLTransform
);
objDocLast
}
#endregion
#region 保存文件到輸出模板
private void SaveAs(string outDoc)
{
object objMissing = System
object objOutDoc = outDoc;
objDocLast
ref objOutDoc
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing
ref objMissing //AddBiDiMarks
);
}
#endregion
#region 循環合並多個文件(復制合並重復的文件)
/// <summary> /// 循環合並多個文件(復制合並重復的文件) /// </summary>
/// <param name=
/// <param name=
/// <param name=
public void CopyMerge(string tempDoc
{
object objMissing = Missing
object objFalse = false;
object objTarget = WdMergeTarget
object objUseFormatFrom = WdUseFormattingFrom
try {
//打開模板文件
Open(tempDoc);
foreach (string strCopy in arrCopies)
{ objDocLast
//FileName
ref objTarget
ref objMissing
ref objUseFormatFrom
ref objMissing //AddToRecentFiles
);
objDocBeforeLast = objDocLast;
objDocLast = objApp
if (objDocBeforeLast != null)
{
objDocBeforeLast
ref objFalse
ref objMissing
ref objMissing //RouteDocument
);
}
}
//保存到輸出文件
SaveAs(outDoc);
foreach (Document objDocument in objApp
{
objDocument
ref objFalse
ref objMissing
ref objMissing //RouteDocument
);
}
}
finally
{
objApp
ref objMissing
ref objMissing
ref objMissing //RoutDocument
);
objApp = null;
}
}
/// <summary> /// 循環合並多個文件(復制合並重復的文件) /// </summary>
/// <param name=
/// <param name=
/// <param name=
public void CopyMerge(string tempDoc
{
string[] arrFiles = Directory
CopyMerge(tempDoc
}
#endregion
#region 循環合並多個文件(插入合並文件)
/// <summary> /// 循環合並多個文件(插入合並文件) /// </summary>
/// <param name=
/// <param name=
/// <param name=
public void InsertMerge(string tempDoc
{
object objMissing = Missing
object objFalse = false;
object confirmConversion = false;
object link = false;
object attachment = false;
try
{
//打開模板文件
Open(tempDoc);
foreach (string strCopy in arrCopies)
{
objApp
strCopy
ref objMissing
ref confirmConversion
ref link
ref attachment
);
}
//保存到輸出文件
SaveAs(outDoc);
foreach (Document objDocument in objApp
{
objDocument
ref objFalse
ref objMissing
ref objMissing //RouteDocument
);
}
}
finally
{
objApp
ref objMissing
ref objMissing
ref objMissing //RoutDocument
);
objApp = null;
}
}
/// <summary>
/// 循環合並多個文件(插入合並文件)
/// </summary>
/// <param name=
/// <param name=
/// <param name=
public void InsertMerge(string tempDoc
{
string[] arrFiles = Directory
InsertMerge(tempDoc
}
#endregion
}
}
From:http://tw.wingwit.com/Article/program/ASP/201311/21785.html