合并Word文档(办公室互操作和放大器; .NET),保持格式化 [英] Merge Word Documents (Office Interop & .NET), Keeping Formatting

查看:216
本文介绍了合并Word文档(办公室互操作和放大器; .NET),保持格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些困难,都融合在一起使用Microsoft Office互操作程序集(Office 2007中)和ASP.NET 3.5多个Word文档。我能够合并文件,但我的一些格式丢失(即字体和图像)。

I'm having some difficulty merging multiple word documents together using Microsoft Office Interop Assemblies (Office 2007) and ASP.NET 3.5. I'm able to merge the documents, but some of my formatting is missing (namely the fonts and images).

我目前的合并code如下图所示。

My current merge code is shown below.

private void CombineDocuments() {
        object wdPageBreak = 7;
        object wdStory = 6;
        object oMissing = System.Reflection.Missing.Value;
        object oFalse = false;
        object oTrue = true;
        string fileDirectory = @"C:\documents\";

        Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document wDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        string[] wordFiles = Directory.GetFiles(fileDirectory, "*.doc");
        for (int i = 0; i < wordFiles.Length; i++) {
            string file = wordFiles[i];
            wDoc.Application.Selection.Range.InsertFile(file, ref oMissing, ref oMissing, ref oMissing, ref oFalse);
            wDoc.Application.Selection.Range.InsertBreak(ref wdPageBreak);
            wDoc.Application.Selection.EndKey(ref wdStory, ref oMissing);
        }
        string combineDocName = Path.Combine(fileDirectory, "Merged Document.doc");
        if (File.Exists(combineDocName))
            File.Delete(combineDocName);
        object combineDocNameObj = combineDocName;
        wDoc.SaveAs(ref combineDocNameObj, ref m_WordDocumentType, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    } 

我不关心这必然是如何完成的。它可以通过PDF输出,如果它必须这样做。我只是想格式化结转。

I don't care necessarily how this is accomplished. It could output via PDF if it had to. I just want the formatting to carry over.

推荐答案

虽然增加你跳过模板名称,这是为什么UT的缺少格式的文件。

While adding the documents you are skipping the Template name, that why ut's missing the format.

它应该看起来像

string defaultTemplate="your template name with full path"; 

默认模板名称

string defaultTemplate="Normal.dot";

wordApplication.Documents.Add(ref defaultTemplate,............

使用这个链接作为参考:<一href=\"http://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple-microsoft-word-documents-in-c.aspx\" rel=\"nofollow\">http://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple-microsoft-word-documents-in-c.aspx

Use this link as Ref: http://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple-microsoft-word-documents-in-c.aspx

这篇关于合并Word文档(办公室互操作和放大器; .NET),保持格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆