5

我有以下代码:

using (var doc = WordprocessingDocument.Open(filename, true))
{
    ....
}

其中字符串文件名是 docx 文件的有效路径。

但是调用 Open 会导致以下 InvalidOperationException:Specified part does not exist in the package.

编辑:我认为问题是由于我试图打开的 docx 文件无效,所以我将发布 xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
    <w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:p w:rsidRPr="005501CD" w:rsidR="00217D87" w:rsidP="005501CD" w:rsidRDefault="005501CD">
            <w:r>
                <w:t xml:space="preserve">Hello </w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:noProof />
                </w:rPr>
                <w:t>John</w:t>
            </w:r>
            <w:r>
                <w:t xml:space="preserve"> </w:t>
            </w:r>
            <w:r>
                <w:rPr>
                    <w:noProof />
                </w:rPr>
                <w:t>Smith</w:t>
            </w:r>
            <w:r>
                <w:t>,</w:t>
            </w:r>
            <w:bookmarkStart w:name="_GoBack" w:id="0" />
            <w:bookmarkEnd w:id="0" />
        </w:p>
        <w:sectPr w:rsidRPr="005501CD" w:rsidR="00217D87">
            <w:pgSz w:w="11906" w:h="16838" />
            <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0" />
            <w:cols w:space="708" />
            <w:docGrid w:linePitch="360" />
        </w:sectPr>
    </w:body>
</w:document>

其他 docx 文件可以正常打开,但我怀疑我的邮件合并代码中的错误导致生成无效的 docx 文件。

4

3 回答 3

6

事实证明,问题是由于缺少 xml 文件。如果其他人遇到类似问题,请检查以下内容:

  1. 将 yourfile.docx 重命名为 yourfile.zip
  2. 提取 zip 文件并查看名为[Content-Types].xml
  3. 检查[Content-Types].xml您的 zip 文件中是否存在列出的所有 xml 文件。

就我而言,我的[Content-Types].xml文件中有以下行:

<Override PartName="/word/recipientData.xml" ContentType="application/vnd.ms-word.mailMergeRecipientData+xml"/>

但我错过recipientData.xml了 word 目录中的文件。

于 2012-07-04T15:42:25.157 回答
1

在您在上面发布的 XML 中,您缺少</w:document>结束标记。这将导致找不到文档部分,这可能是您看到该错误消息的原因。

于 2012-07-03T16:20:53.200 回答
1

就我而言,我添加了一个 .png 媒体,但未在以下行中添加以下行[Content_Types].xml

<Default Extension="png" ContentType="image/png"/>
于 2019-12-20T14:17:09.407 回答