C#實(shí)現(xiàn)合并多個(gè)word文檔的方法
本文實(shí)例講述了C#實(shí)現(xiàn)合并多個(gè)word文檔的方法,是非常具有實(shí)用價(jià)值的技巧。分享給大家供大家參考。
具體實(shí)現(xiàn)方法如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office.Interop.Word;
using System.Reflection;
using System.IO;
using System.Diagnostics;
namespace driverexam.WordReport
{
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 循環(huán)合并多個(gè)文件(復(fù)制合并重復(fù)的文件)
/// <summary>
/// 循環(huán)合并多個(gè)文件(復(fù)制合并重復(fù)的文件)
/// </summary>
/// <param name="tempDoc">模板文件</param>
/// <param name="arrCopies">需要合并的文件</param>
/// <param name="outDoc">合并后的輸出文件</param>
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;
}
}
/// <summary>
/// 循環(huán)合并多個(gè)文件(復(fù)制合并重復(fù)的文件)
/// </summary>
/// <param name="tempDoc">模板文件</param>
/// <param name="arrCopies">需要合并的文件</param>
/// <param name="outDoc">合并后的輸出文件</param>
public void CopyMerge(string tempDoc, string strCopyFolder, string outDoc)
{
string[] arrFiles = Directory.GetFiles(strCopyFolder);
CopyMerge(tempDoc, arrFiles, outDoc);
}
#endregion
#region 循環(huán)合并多個(gè)文件(插入合并文件)
/// <summary>
/// 循環(huán)合并多個(gè)文件(插入合并文件)
/// </summary>
/// <param name="tempDoc">模板文件</param>
/// <param name="arrCopies">需要合并的文件</param>
/// <param name="outDoc">合并后的輸出文件</param>
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;
}
}
/// <summary>
/// 循環(huán)合并多個(gè)文件(插入合并文件)
/// </summary>
/// <param name="tempDoc">模板文件</param>
/// <param name="arrCopies">需要合并的文件</param>
/// <param name="outDoc">合并后的輸出文件</param>
public void InsertMerge(string tempDoc, string strCopyFolder, string outDoc)
{
string[] arrFiles = Directory.GetFiles(strCopyFolder);
InsertMerge(tempDoc, arrFiles, outDoc);
}
#endregion
}
}
相信本文所述對(duì)大家的C#程序設(shè)計(jì)有一定的借鑒價(jià)值。
- C# WORD操作實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)通過(guò)模板自動(dòng)創(chuàng)建Word文檔的方法
- C# Word 類庫(kù)的深入理解
- asp.net(c#)下讀取word文檔的方法小結(jié)
- 比較全的一個(gè)C#操作word文檔示例
- 使用c#在word文檔中創(chuàng)建表格的方法詳解
- c#開發(fā)word批量轉(zhuǎn)pdf源碼分享
- C#采用OpenXml實(shí)現(xiàn)給word文檔添加文字
- C#采用OpenXml給word里面插入圖片
- 使用C#實(shí)現(xiàn)在word中插入頁(yè)眉頁(yè)腳的方法
- C#獲取Word文檔中所有表格的實(shí)現(xiàn)代碼分享
- C#操作word的方法示例
相關(guān)文章
C# MeasureString測(cè)量字符串函數(shù)的使用方法
這篇文章主要介紹了C# MeasureString測(cè)量字符串函數(shù)的使用方法,需要的朋友可以參考下2014-10-10
C++調(diào)用C#的DLL程序?qū)崿F(xiàn)方法
本文通過(guò)例子,講述了C++調(diào)用C#的DLL程序的方法,作出了以下總結(jié),具有一定的參考價(jià)值,下面就讓我們一起來(lái)學(xué)習(xí)吧2015-10-10
程序中兩個(gè)Double類型相加出現(xiàn)誤差的解決辦法
本篇文章介紹了,程序中兩個(gè)Double類型相加出現(xiàn)誤差的解決辦法。需要的朋友參考下2013-04-04
C#?線程切換后上下文都去了哪里(.NET高級(jí)調(diào)試分析)
總會(huì)有一些朋友問一個(gè)問題,在 Windows 中線程做了上下文切換,請(qǐng)問被切的線程他的寄存器上下文都去了哪里?這個(gè)問題其實(shí)比較底層,如果對(duì)操作系統(tǒng)沒有個(gè)體系層面的理解以及做過(guò)源碼分析,其實(shí)很難說(shuō)明白,這篇我們就從.NET高級(jí)調(diào)試的角度分析,需要的朋友可以參考下2023-12-12
Unity的OnOpenAsset實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity的OnOpenAsset實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
C#獲取變更過(guò)的DataTable記錄的實(shí)現(xiàn)方法
這篇文章主要介紹了C#獲取變更過(guò)的DataTable記錄的實(shí)現(xiàn)方法,對(duì)初學(xué)者很有學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下2014-08-08

