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

如题所述

下载spire.doc, åœ¨ç¨‹åºç”¨å¼•ç”¨spire.doc.dll

合并Word文档:

using Spire.Doc;

namespace Merge_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //获取文档路径
            string filePath_1 = @"C:\Users\Administrator\Desktop\Word_1.docx";
            string filePath_2 = @"C:\Users\Administrator\Desktop\Word_2.docx";

            //加载文档1到Document对象
            Document doc= new Document(filePath_1);

            //使用InsertTextFromFile方法将文档2合并到新文档
            doc.InsertTextFromFile(filePath_2, FileFormat.Docx2013);

            //保存文档
            doc.SaveToFile("合并文档.docx", FileFormat.Docx2013);
        }
    }
}

拆分Word文档:

1、按分节符拆分文档

using Spire.Doc;

namespace Split_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化Document对象
            Document document = new Document();

            //载入待拆分的Word文档
            document.LoadFromFile("测试文档.docx");

            Document newWord;
            for (int i = 0; i < document.Sections.Count; i++)
            {
                //每有一个section就创建一个新的文档
                newWord = new Document();
                //复制section内容到新文档
                newWord.Sections.Add(document.Sections[i].Clone());
                //保存文档
                newWord.SaveToFile(String.Format("分节符拆分的结果文档_{0}.docx", i));
            }
        }
    }
}

2、按分页符拆分文档

using Spire.Doc;
using Spire.Doc.Documents;

namespace Split_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化Document对象
            Document original = new Document();
            //载入待拆分的Word文档
            original.LoadFromFile(@"C:\Users\Administrator\Desktop\template.docx");

            //实例化一个新的文档并添加新章节
            Document newWord = new Document();
            Section section = newWord.AddSection();

            int index = 0;
            //根据章节,段落的层次由大到小依次遍历文档元素,复制内容到新的文档
            foreach (Section sec in original.Sections)
            {
                foreach (DocumentObject obj in sec.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        Paragraph para = obj as Paragraph;
                        section.Body.ChildObjects.Add(para.Clone());

                        foreach (DocumentObject parobj in para.ChildObjects)
                        {
                            //找到段落中的分页符,保存到新文档
                            if (parobj is Break && (parobj as Break).BreakType == BreakType.PageBreak)
                            {
                                int i = para.ChildObjects.IndexOf(parobj);
                                section.Body.LastParagraph.ChildObjects.RemoveAt(i);
                                newWord.SaveToFile(String.Format("分页符拆分的结果文档_{0}.docx", index), FileFormat.Docx);
                                index++;
                                //一个文档完成之后新建一个文档
                                newWord = new Document();
                                section = newWord.AddSection();
                                //复制上一个分页符所在的段落的所有内容到新文档
                                section.Body.ChildObjects.Add(para.Clone());
                                //如果新文档第一段(也就是刚刚复制的那一段)没有子元素,
                                //则把文档的第一个子元素删除
                                if (section.Paragraphs[0].ChildObjects.Count == 0)
                                {
                                    section.Body.ChildObjects.RemoveAt(0);
                                }
                                else
                                {
                                    //如果有内容则删除分页符之前的所有内容
                                    while (i >= 0)
                                    {
                                        section.Paragraphs[0].ChildObjects.RemoveAt(i);
                                        i--;
                                    }
                                }
                            }
                        }
                    }
                    if (obj is Table)
                    {
                        section.Body.ChildObjects.Add(obj.Clone());
                    }
                }
            }
            newWord.SaveToFile(String.Format("分页符拆分的结果文档_{0}.docx", index), FileFormat.Docx);
        }
    }
}
温馨提示:答案为网友推荐,仅供参考