【问题标题】:Cannot open OpenXML docx file created manually无法打开手动创建的 OpenXML docx 文件
【发布时间】:2017-01-05 12:52:14
【问题描述】:

我一直在阅读 Wouter van Vugt 的“Open XML。标记解释”。 “Wordprocessing ML”一章包含相当简单的手动创建简单 docx 文件的演练,方法是创建几个 xml 部分文件,将其压缩并重命名为 *.docx。
我已经严格按照说明进行操作,但是当我尝试使用 Open XML SDK Productivity Tool 打开最终的 *.zip 文件时出现错误:“无法打开文件:不支持指定的文件类型”。
我尝试使用 MS Word 创建一个 docx 文件,将其重命名为 *.zip 并使用生产力工具打开它。有效。这个文件中的部分比手动创建的要多得多,但描述性部分实际上是相同的。 我做错了什么?
P.S.:是的,我知道 OOXML 应该是通过一些与 .NET 兼容的代码创建的,而不是通过手动创建 XML 内容。但我试图理解它的结构,但我不明白。

创建文件的内容:

[内容类型].xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
    <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
    <Default Extension="xml" ContentType="application/xml" />
    <Override PartName="/Document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.mainxml" />
</Types>

文档.xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:body>
        <w:p>
            <w:r>
                <w:t>Hello World!</w:t>
            </w:r>
        </w:p>
    </w:body>
</w:document>

.rels:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
    <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="Document.xml" />
</Relationships>

【问题讨论】:

    标签: openxml openxml-sdk


    【解决方案1】:

    假设文件结构如下:

    _rels
        .rels
    [Content_Types].xml
    Document.xml
    

    那么它可能只是 [Content_Types].xml 中最后一个“ContentType”末尾缺少一个“+”:

    <Override PartName="/Document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" />
    

    (使用 'ma​​in+xml' 而不是 'ma​​inxml')。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多