Spire.Doc for .NET如何实现多个Word文档的合并

作者:控件中国网   出处:控件中国网   2015-06-08 11:01:51   阅读:41

Spire.Doc for .NET主要用于开发人员进行word文档操作和处理,主要进行word文档的创建、打开、和保存,邮件的合并,安全、格式化以及和其他多种文档之间的转换等,支持word文档从97-2013版本,并且可以用于.NET/WPF和silverlight平台下。

要实现多个文档的合并,在该产品中只需要几句代码即可实现,首先要对合并的单个文档进行初始化,然后得到文档的片段,然后使用控件提供的Document.Sections.Add在指定的片段后加入要合并的文档即可,具体代码如下:

[C#]

using Spire.Doc;

namespace MergeDocument
{
    class DocxMerge
    {
        static void Main(string[] args)
        {
            //Load Document1 and Document2
            Document DocOne = new Document();
            DocOne.LoadFromFile(@"E:\Work\Document\welcome.docx", FileFormat.Docx);
            Document DocTwo = new Document();
            DocTwo.LoadFromFile(@"E:\Work\Document\New Zealand.docx", FileFormat.Docx);

            //Merge
            foreach (Section sec in DocTwo.Sections)
            {
                DocOne.Sections.Add(sec.Clone());
            }

            //Save and Launch
            DocOne.SaveToFile("Merge.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Merge.docx");
        }
    }
}

[VB.NET]
Imports Spire.Doc

Namespace MergeDocument
    Friend Class DocxMerge
        Shared Sub Main(ByVal args() As String)
            'Load Document1 and Document2
            Dim DocOne As New Document()
            DocOne.LoadFromFile("E:\Work\Document\welcome.docx", FileFormat.Docx)
            Dim DocTwo As New Document()
            DocTwo.LoadFromFile("E:\Work\Document\New Zealand.docx", FileFormat.Docx)

            'Merge
            For Each sec As Section In DocTwo.Sections
                DocOne.Sections.Add(sec.Clone())
            Next sec

            'Save and Launch
            DocOne.SaveToFile("Merge.docx", FileFormat.Docx)
            System.Diagnostics.Process.Start("Merge.docx")
        End Sub
    End Class
End Namespace

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat