C# 如何合并、拆分Word文档

100阅读 0评论2018-07-03 Mia张
分类:C#/.net

概述

出于方便文档管理、存储、传输等目的,我们常会想要将某些文档拆分为多个子文档,或者将多个文档合并为一个文档。在本文中,将介绍对Word文档进行拆分、合并的方法。下面的示例中将包含以下要点:

1. 合并Word文档

  1.1 新建一页合并到文档

  1.2紧接上文合并到文档

2. 拆分Word文档

  2.1 按分节符拆分

2.2 按分页符拆分


所用工具:




示例操作:

一、合并Word文档

(一)以新建一页合并到文档

C#

  1. using Spire.Doc;

  2. namespace MergeWord_Doc
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             //创建两个文档,加载需要合并的文件
  9.             Document doc1 = new Document(@"C:\Users\Administrator\Desktop\TradeNegotiation.docx");
  10.             Document doc2 = new Document(@"C:\Users\Administrator\Desktop\DisputeSettlement.docx");

  11.             //调用InsertTextFromFile()方法,将文档2合并到文档1
  12.             string fileName = @"C:\Users\Administrator\Desktop\DisputeSettlement.docx";
  13.             doc1.InsertTextFromFile(fileName, FileFormat.Docx2013);

  14.             //保存文件
  15.             doc1.SaveToFile("MergedDocument.docx", FileFormat.Docx2013);
  16.         }
  17.     }
  18. }

调试运行该项目,生成文件,如下图所示:

(二)紧接上文合并到文档

C#


  1. using Spire.Doc;
  2. using Spire.Doc.Documents;

  3. namespace MergeWord2_Doc
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //创建两个文档,并加载需要合并的两个文件
  10.             Document doc1 = new Document(@"C:\Users\Administrator\Desktop\TradeNegotiation.docx");
  11.             Document doc2 = new Document(@"C:\Users\Administrator\Desktop\DisputeSettlement.docx");
  12.             //获取文档1的最后一个Section
  13.             Section lastSection = doc1.LastSection;
  14.             //遍历文档2中的所有section,复制所有section到文档1
  15.             foreach (Section section in doc2.Sections)
  16.             {
  17.                 foreach (Paragraph paragraph in section.Paragraphs)
  18.                 {
  19.                     lastSection.Paragraphs.Add(paragraph.Clone() as Paragraph);
  20.                 }
  21.             }
  22.             //将合并的文档另存为一个新文档
  23.             doc1.SaveToFile("Merged.docx", FileFormat.Docx2013);
  24.         }
  25.     }
  26. }

合并效果:


二、拆分Word文档

(一)按分节符拆分

C#


  1. using Spire.Doc;
  2. using System;

  3. namespace SplitWord_Doc
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //创建一个Document类对象,并加载需要拆分的文档
  10.             Document document = new Document();
  11.             document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");
  12.             //实例化Document对象
  13.             Document newWord;
  14.             //遍历文档所有section,复制文档每个section并分别保存到新建的文档,同时将拆分的文档保存到指定路径
  15.             for (int i = 0; i < document.Sections.Count; i++)
  16.             {
  17.                 newWord = new Document();
  18.                 newWord.Sections.Add(document.Sections[i].Clone());
  19.                 newWord.SaveToFile(String.Format(@"results\out_{0}.docx", i));
  20.             }

  21.         }
  22.     }
  23. }

效果展示:


(二)按分页符拆分

C#


  1. using System;
  2. using Spire.Doc;
  3. using Spire.Doc.Documents;

  4. namespace Split_Word_Document_by_Page_Break
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //实例化一个Document类,加载文档
  11.             Document original = new Document();
  12.             original.LoadFromFile(@"C:\Users\Administrator\Desktop\test.docx");
  13.             //实例化Document类对象,并添加section
  14.             Document newWord = new Document();
  15.             Section section = newWord.AddSection();
  16.             //根据分页来拆分文档
  17.             int index = 0;
  18.             //遍历文档所有section
  19.             foreach (Section sec in original.Sections)
  20.             {
  21.                 //遍历文档所有子对象
  22.                 foreach (DocumentObject obj in sec.Body.ChildObjects)
  23.                 {
  24.                     if (obj is Paragraph)
  25.                     {
  26.                         Paragraph para = obj as Paragraph;
  27.                         //复制并添加原有段落对象到新文档
  28.                         section.Body.ChildObjects.Add(para.Clone());
  29.                         //遍历所有段落子对象
  30.                         foreach (DocumentObject parobj in para.ChildObjects)
  31.                         {
  32.                             if (parobj is Break && (parobj as Break).BreakType == BreakType.PageBreak)
  33.                             {
  34.                                 //获取段落分页并移除,保存新文档到文件夹
  35.                                 int i = para.ChildObjects.IndexOf(parobj);
  36.                                 section.Body.LastParagraph.ChildObjects.RemoveAt(i);
  37.                                 newWord.SaveToFile(String.Format("results/out-{0}.docx", index), FileFormat.Docx);
  38.                                 index++;
  39.                                 //实例化Document类对象,添加section,将原文档段落的子对象复制到新文档
  40.                                 newWord = new Document();
  41.                                 section = newWord.AddSection();
  42.                                 section.Body.ChildObjects.Add(para.Clone());
  43.                                 if (section.Paragraphs[0].ChildObjects.Count == 0)
  44.                                 {
  45.                                     //移除第一个空白段落
  46.                                     section.Body.ChildObjects.RemoveAt(0);
  47.                                 }
  48.                                 else
  49.                                 {
  50.                                     //删除分页符前的子对象
  51.                                     while (i >= 0)
  52.                                     {
  53.                                         section.Paragraphs[0].ChildObjects.RemoveAt(i);
  54.                                         i--;
  55.                                     }
  56.                                 }
  57.                             }
  58.                         }
  59.                     }
  60.                     //若对象为表格,则添加表格对象到新文档
  61.                     if (obj is Table)
  62.                     {
  63.                         section.Body.ChildObjects.Add(obj.Clone());
  64.                     }
  65.                 }
  66.             }
  67.             //拆分后的新文档保存至指定文档
  68.             newWord.SaveToFile(String.Format("results/out-{0}.docx", index), FileFormat.Docx);
  69.         }
  70.     }
  71. }

拆分效果:



阅读结束。

如需转载,请注明出处!



上一篇:C# /VB.NET 插入、修改、删除Word批注
下一篇:C#/VB.NET 操作Word批注(二)——如何插入图片、读取、回复Word批注内容