C#實(shí)現(xiàn)合并多個word文檔的方法
本文實(shí)例講述了C#實(shí)現(xiàn)合并多個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)合并多個文件(復(fù)制合并重復(fù)的文件)
/// <summary>
/// 循環(huán)合并多個文件(復(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)合并多個文件(復(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)合并多個文件(插入合并文件)
/// <summary>
/// 循環(huán)合并多個文件(插入合并文件)
/// </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)合并多個文件(插入合并文件)
/// </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
}
}
相信本文所述對大家的C#程序設(shè)計(jì)有一定的借鑒價(jià)值。
- C# WORD操作實(shí)現(xiàn)代碼
- C#實(shí)現(xiàn)通過模板自動創(chuàng)建Word文檔的方法
- C# Word 類庫的深入理解
- asp.net(c#)下讀取word文檔的方法小結(jié)
- 比較全的一個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中插入頁眉頁腳的方法
- C#獲取Word文檔中所有表格的實(shí)現(xiàn)代碼分享
- C#操作word的方法示例
相關(guān)文章
C# HttpClient上傳文件并附帶其它參數(shù)方式
這篇文章主要介紹了C# HttpClient上傳文件并附帶其它參數(shù)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
C#中LinkedList<T>的存儲結(jié)構(gòu)詳解
這篇文章主要介紹了深度解析C#中LinkedList<T>的存儲結(jié)構(gòu),本文將從鏈表的基礎(chǔ)特性、C#中LinkedList的底層實(shí)現(xiàn)邏輯,.NET的不同版本對于Queue的不同實(shí)現(xiàn)方式的原因分析等幾個視角進(jìn)行簡單的解讀,需要的朋友可以參考下2023-12-12
使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
中秋國慶節(jié)眼看到跟前了,很多商城都借此機(jī)會搞促銷活動,什么滿200減80送優(yōu)惠券等活動,基于后臺程序是怎么實(shí)現(xiàn)的呢?下面腳本之家小編帶領(lǐng)大家一起學(xué)習(xí)吧2015-09-09
在C#使用字典存儲事件示例及實(shí)現(xiàn)自定義事件訪問器
這篇文章主要介紹了在C#使用字典存儲事件示例及實(shí)現(xiàn)自定義事件訪問器的方法,是C#事件編程中的基礎(chǔ)知識,需要的朋友可以參考下2016-02-02
C# 通過反射初探ORM框架的實(shí)現(xiàn)原理(詳解)
下面小編就為大家分享一篇C# 通過反射初探ORM框架的實(shí)現(xiàn)原理詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
C# KeyUp事件中MessageBox的回車(Enter)鍵回調(diào)問題解決方案
這篇文章主要介紹了C# KeyUp事件中MessageBox的回車(Enter)鍵回調(diào)問題解決方案,需要的朋友可以參考下2014-07-07
C#實(shí)現(xiàn)日期格式轉(zhuǎn)換的公共方法類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)日期格式轉(zhuǎn)換的公共方法類,結(jié)合完整實(shí)例形式分析了C#針對各種常見日期格式的轉(zhuǎn)換方法,涉及C#字符串、日期、時間相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
C#實(shí)現(xiàn)Winform小數(shù)字鍵盤模擬器
本文主要介紹了C#實(shí)現(xiàn)Winform小數(shù)字鍵盤模擬器,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11

