0

私のコードは、docx ドキュメントを作成し、複数のドキュメントからパーツを取得して 1 つにマージすることを目的としています。

private void CopyContent(WordprocessingDocument sourceDoc, WordprocessingDocument targetDoc)
{
    // copy parts from source document to new document
    foreach (var part in sourceDoc.Parts)
        targetDoc.AddPart(part.OpenXmlPart, part.RelationshipId);

    using (var sr = new StreamReader(sourceDoc.MainDocumentPart.GetStream()))
    using (var sw = new StreamWriter(targetDoc.MainDocumentPart.GetStream(FileMode.Create)))
    {
        sw.Write(sr.ReadToEnd());
    }
    targetDoc.MainDocumentPart.Document.Save();
}

コードは抜粋のみで、docxOffice 2013 で作成した場合は正常に動作しますが、ドキュメントがdocOffice 2013 で docx として保存された (古いバージョンの Office で作成された)場合は失敗しますtargetDoc.MainDocumentPart.Document.Save()。実行すると、次のエラーが発生します。

{"The root XML element \"http://purl.oclc.org/ooxml/wordprocessingml/main:document\" in the part is incorrect. The expected root XML element is: \"http://schemas.openxmlformats.org/wordprocessingml/2006/main:document\"."}

どういう意味ですか?どうすれば解決できますか?

4

1 に答える 1

0

処理する前に.docファイルを変換する必要があります。それを達成するには、ライブラリ.docxを使用できます。Microsoft.Office.Interop.Word

ここにいくつかの例

于 2016-03-22T13:18:30.593 に答える