使用C#实现轻松合并多份Word
很多项目需要把多份 Word 文档汇总到一份成品里:例如把多个章节的材料拼成一个报告、把不同来源的稿件合并后交付。与此同时,“合并后格式是否正常、分页是否符合预期、代码是否足够简洁”往往决定了方案能否落地。
下面介绍两种常见的合并策略,均使用 Spire.Doc for .NET 实现:
- 方法一 :将后文档整体追加到前文档后面(通常带来“从新页开始”的整体插入体验)
- 方法二 :遍历后文档的 Section,将其内容对象克隆后追加到前文档最后一个 Section,适合更精细的结构控制
Spire.Doc 用途与安装方法(Section 简介)
Spire.Doc 是用于 .NET 读取、编辑与生成 Word 文档(DOC/DOCX)的组件库。它提供面向文档结构的 API,例如加载文档、插入内容、遍历 Section/Body/对象集合、复制文档元素、保存为 DOCX 等。相比自行解析 docx 压缩包与 XML,使用它可以显著降低开发成本。
安装方式(推荐 NuGet):
- 在 Visual Studio 打开项目
- 右键项目 → 管理 NuGet 程序包
- 搜索并安装 Spire.Doc for .NET
- 在代码中引用命名空间:
using Spire.Doc;
完成安装后,就能直接通过 Document 类来加载与操作 Word 文件。
方法一:整体插入到末尾(从新页开始的常见效果)
当目标是“把 Doc2 作为一个整体接到 Doc1 后面”,同时希望合并结果更接近 Word 中的“插入文档/追加内容”的直观体验时,可以使用 InsertTextFromFile。
实现思路:
- 创建
Document对象作为承载文档 - 加载主文档(Doc1)
- 将另一个 Word 文件(Doc2)插入到主文档后
- 保存为新的合并文件
示例代码:
using Spire.Doc;
namespace MergeWord
{
class Program
{
static void Main(string[] args)
{
// Create a Document instance
Document document = new Document();
// Load the original Word document
document.LoadFromFile("Doc1.docx", FileFormat.Docx);
// Insert another Word document entirely to the original document
document.InsertTextFromFile("Doc2.docx", FileFormat.Docx);
// Save the result document
document.SaveToFile("MergedWord.docx", FileFormat.Docx);
}
}
}
这种方式的特点
- 代码量少、开发速度快
- 更偏“整体追加”,适合按文档边界合并
- 对 Section 级别结构的细节控制相对少,但通常足够满足常见汇总需求
方法二:追加到第一个文档最后一个 Section(结构更可控)
如果合并要求更精细,例如尽量让内容与前文档“同节连续”,或者需要把后文档的内容对象逐个追加到前文档末尾,那么可采用第二种方案:遍历后文档的 Sections,再遍历每个 Section 的 Body.ChildObjects,将对象 Clone() 后加入前文档最后一个 Section。
实现思路:
- 分别加载
doc1与doc2 - 遍历
doc2.Sections - 对每个 section,遍历其
section.Body.ChildObjects - 将克隆后的对象添加到
doc1.LastSection.Body.ChildObjects - 保存
doc1作为合并结果
示例代码:
using Spire.Doc;
namespace MergeWord
{
class Program
{
static void Main(string[] args)
{
// Load two Word documents
Document doc1 = new Document("Doc1.docx");
Document doc2 = new Document("Doc2.docx");
// Loop through the second document to get all the sections
foreach (Section section in doc2.Sections)
{
// Loop through the sections of the second document to get their child objects
foreach (DocumentObject obj in section.Body.ChildObjects)
{
// Get the last section of the first document
Section lastSection = doc1.LastSection;
// Add all child objects to the last section of the first document
lastSection.Body.ChildObjects.Add(obj.Clone());
}
}
// Save the result document
doc1.SaveToFile("MergeDocuments.docx", FileFormat.Docx);
}
}
}
这种方式的特点
- 更接近“把内容对象直接拼到最后一节里”
- 通过
Clone()进行对象复制,避免直接引用引发的问题 - 适合处理包含多节结构、页眉页脚/分页规则等更复杂情况(具体效果仍需根据模板而定)
如何选择
- 只想快速合并,并希望结果接近“从新页开始追加”的直观效果:方法一更省事。
- 需要更强的结构控制,把后文档内容追加到前文档最后一个 Section:方法二更贴合需求。
如果你的文档模板包含页眉页脚、不同方向页面(横/竖版)或复杂节配置,建议优先用方法二做验证;如果只是普通文本与表格拼接,方法一通常更快更稳定。
需要的话,我也可以基于这两种方法给出“分页符/节属性”可能导致的差异排查清单,帮助你在真实模板上快速定位合并后的格式问题。
知识扩展
在 C# 中合并 Word 文档有多种实现方式,从完全免费的 OpenXML SDK 到 API 简洁的 商业库,各有优劣。下表对比了几种主流方案,方便你快速选型:
| 方案 | 是否免费 | 开发效率 | 代码复杂度 | 特殊能力 | 适用场景 |
|---|---|---|---|---|---|
| OpenXML SDK | ✅ 免费 | 较低 | 高 | 需手动处理底层XML和分节符 | 零成本、有技术余力的项目 |
| DocX | ✅ 免费 | 中 | 中 | 不支持加密/旧版.doc文件 | 非商业个人项目或小规模免费场景 |
| Spire.Doc | ❌ 商业(免费版有限制) | 高 | 极低 | 支持所有Word格式、Document.AppendDocument()一行式分页合并 | 快速开发、商业项目 |
| Aspose.Words | ❌ 商业 | 高 | 极低 | 工业级稳定、段落分页控制、层次化结构导入 | 高度格式保真、企业级应用 |
| GroupDocs.Merger | ❌ 商业 | 高 | 中 | 支持跨格式合并 (Word/PDF/PPT) | 多格式文档处理平台 |
Spire.Doc:最简洁方案
代码量极少,使用 Document.AppendDocument() 方法即可直接合并。
安装:
Install-Package Spire.Doc
核心代码:
using Spire.Doc;
Document doc = new Document("Document1.docx");
doc.AppendDocument(new Document("Document2.docx"), FileFormat.Docx2013);
doc.AppendDocument(new Document("Document3.docx"), FileFormat.Docx2013);
doc.SaveToFile("MergedDocument.docx", FileFormat.Docx2013);Aspose.Words:工业级方案
支持 段落分页控制、精确分节管理,也能高效导入多个文档。
安装:
dotnet add package Aspose.Words
基础合并:
using Aspose.Words;
Document mergedDoc = new Document("Doc1.docx");
mergedDoc.AppendDocument(new Document("Doc2.docx"), ImportFormatMode.KeepSourceFormatting);
mergedDoc.Save("Merged.docx");高级合并(逐页追加、目标文档完整保留):
推荐使用 Document.AppendDocument()。同时为了规避 ASP.NET 的权限限制,商业开发也可考虑 MemoryStream 在内存中合并多个文档,确保格式和图表不丢失。
using Aspose.Words;
Document mergedDoc = new Document();
var stream1 = new MemoryStream(File.ReadAllBytes("Doc1.docx"));
var stream2 = new MemoryStream(File.ReadAllBytes("Doc2.docx"));
mergedDoc.AppendDocument(new Document(stream1), ImportFormatMode.KeepSourceFormatting);
mergedDoc.AppendDocument(new Document(stream2), ImportFormatMode.KeepSourceFormatting);
mergedDoc.Save("Merged.docx");GroupDocs.Merger:跨格式方案
安装:
dotnet add package GroupDocs.Merger
代码示例:
using GroupDocs.Merger;
var merger = new Merger("Document1.docx");
merger.Join("Document2.docx");
merger.Join("Document3.docx");
merger.Save("MergedDocument.docx");OpenXML SDK:免费、强控底层
优势是完全免费且支持细粒度控制,但需手动处理分节符冲突。
安装:
dotnet add package DocumentFormat.OpenXml
核心代码(手动分页):
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;
using System.Linq;
using A = DocumentFormat.OpenXml;
public static void MergeWordDocuments(string outputPath, params string[] inputPaths)
{
using (WordprocessingDocument outputDoc = WordprocessingDocument.Create(outputPath, WordprocessingDocumentType.Document))
{
Body outputBody = outputDoc.MainDocumentPart.Document.Body;
outputBody.RemoveAllChildren();
bool isFirst = true;
foreach (string inputPath in inputPaths)
{
using (WordprocessingDocument inputDoc = WordprocessingDocument.Open(inputPath, false))
{
Body inputBody = inputDoc.MainDocumentPart.Document.Body;
foreach (var element in inputBody.Elements())
{
outputBody.AppendChild(element.CloneNode(true));
}
if (!isFirst)
{
outputBody.AppendChild(new Paragraph(new Run(new Break() { Type = BreakValues.Page })));
}
isFirst = false;
}
}
outputDoc.MainDocumentPart.Document.Save();
}
}关键注意事项
- 分页符处理:免费方案 (OpenXML) 需手动在新文档间插入
Break保证各文档分页独立。 - 格式兼容性:
.doc旧格式需注意Spire.Doc与Aspose.Words支持较好;DocX不支持.doc格式。 - 许可证限制:免费版商业库通常有水印、页数限制,评估时务必先试用确认。如 Free Spire.Doc 存在页数限制。
- 内存优化:大量文档合并时建议使用 流式处理 (MemoryStream),保证内存稳定。
提供更多 批量合并逻辑 或 服务器端部署细节 可定制方案。
到此这篇关于使用C#实现轻松合并多份Word的文章就介绍到这了,更多相关C#合并多个Word内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!


最新评论