合并 Word 文档(Office Interop 和 .NET),保持格式

发布于 2024-08-26 15:21:44 字数 1662 浏览 4 评论 0原文

我在使用 Microsoft Office Interop Assemblies (Office 2007) 和 ASP.NET 3.5 将多个 Word 文档合并在一起时遇到一些困难。我可以合并文档,但缺少一些格式(即字体和图像)。

我当前的合并代码如下所示。

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'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).

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);
    } 

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百合的盛世恋 2024-09-02 15:21:44

在添加文档时,您会跳过模板名称,这就是为什么 ut 缺少格式。

它应该看起来像

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

默认模板名称

string defaultTemplate="Normal.dot";

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

使用此链接作为参考:http://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple -microsoft-word-documents-in-c.aspx

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

It should look like

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

OR

Default template name

string defaultTemplate="Normal.dot";

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文