首页 文章

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

提问于
浏览
3

我在使用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输出 . 我只是希望格式化继续下去 .

1 回答

相关问题