使用 OpenXML 合并多个 Word 文档

问题描述 投票:0回答:1

我正在将文档与以下代码合并。包含文本的简单文档合并得很好,我的 RichEditControl 可以加载最终文档并显示它。但是,其他一些带有精美图形的文档未加载。当我调用 LoadDocument 时出现异常。知道可以做什么来解决这个问题吗?

    public void MergeWordDocuments()//string filePathSrc1, string filePathSrc2)
    {
        using (Log.VerboseCall())
        {
            if (SelectedChaptersList == null ||  SelectedChaptersList.Count == 0)
            {
                XtraMessageBox.Show("No Chapter Documents Selected", Properties.Settings.Default.AppTitle,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            SelectedChaptersList.Sort((x, y) => x.ChapterNum.CompareTo(y.ChapterNum));

            string manualFilePath = Path.Combine(mManualOutputDir, "tempManualFileName.docx");
            try
            {
                if (File.Exists(manualFilePath))
                {
                        File.Delete(manualFilePath);
                }
            }
            catch(Exception ex)
            {
                Log.Verbose(ex);
            }

            string firstFilePath = Path.Combine(mManualOutputDir, Path.Combine(SelectedChaptersList[0].FilePath,
                                                                  SelectedChaptersList[0].FileName +
                                                                  SelectedChaptersList[0].FileType));
            File.Copy(firstFilePath, manualFilePath);

            using (WordprocessingDocument destinationDocument = WordprocessingDocument.Open(manualFilePath, true))
            {
                // This is mainPart of the Manual
                MainDocumentPart mainPart = destinationDocument.MainDocumentPart;

                // If the document doesn't have a main document part, add one
                if (mainPart == null)
                {
                    mainPart = destinationDocument.AddMainDocumentPart();
                    new Document(new Body()).Save(mainPart);
                }

                MergeDocument(mainPart, manualFilePath);
            }
        }
    }

    private void MergeDocument(MainDocumentPart mainPart, string manualFilePath)
    {
        using (Log.VerboseCall())
        {
            for (int i = 1; i < SelectedChaptersList.Count; i++)
            {
                string filePath = Path.Combine(SelectedChaptersList[i].FilePath,
                                                SelectedChaptersList[i].FileName + SelectedChaptersList[i].FileType);

                // Open the source document from the list to be merged into manual
                using (WordprocessingDocument sourceDocument = WordprocessingDocument.Open(filePath, false))
                {
                    // Take care of all the necessary imports/merges of styles, fonts, etc.

                    // Import the body content from the source document
                    foreach (var element in sourceDocument.MainDocumentPart.Document.Body.Elements())
                    {
                        mainPart.Document.Body.Append(element.CloneNode(true));
                    }
                }
            }
        }
    }

    private void ManualUserCtrl_VisibleChanged(object sender, EventArgs e)
    {
        using (Log.VerboseCall())
        {
            if (this.Visible == true)
            {
                string manualFilePath = Path.Combine(mManualOutputDir, "tempManualFileName.docx");
                ManualRichEditCtrl.LoadDocument(manualFilePath);
            }
        }
    }

最后 LoadDocument 抛出以下异常:

Exception thrown: 'System.Text.DecoderFallbackException' in mscorlib.dll
Exception thrown: 'System.Text.DecoderFallbackException' in mscorlib.dll
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
Exception thrown: 'System.Exception' in DevExpress.RichEdit.v22.2.Core.dll
Exception thrown: 'System.Exception' in DevExpress.Office.v22.2.Core.dll
Exception thrown: 'System.ArgumentException' in DevExpress.RichEdit.v22.2.Core.dll
c# ms-word openxml
1个回答
0
投票

这可能是可以解决的,但是,目前,我可以通过切换到 Word.Interop 来解决它,它基本上是为此类问题设计的。

© www.soinside.com 2019 - 2024. All rights reserved.