1

I have some code that creates a document to be viewed in MS Word. Some XML is run through XSLT, then added into the OpenXML document. The resulting document looks fine in Word 2010, but won't open in Word 2007, which I need to be able to support. How can I create a valid Word 2007 document?

With Open XML Format SDK 2.0 (version 2.0.5022.0)

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

...

using (WordprocessingDocument package = WordprocessingDocument.Create(outputPath, WordprocessingDocumentType.Document))
{

    package.AddMainDocumentPart();

    MainDocumentPart mainPart = package.MainDocumentPart;

    mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document();

    // code ... XML translation and append to document

    package.MainDocumentPart.Document.Save();
}

.


This post has some useful information:

There are differences between Word 2010 and Word 2007, and they adhere to different versions of the OOXML standard. It is quite possible that you have included content that works in Word 2010, but that Word 2007 can't handle. You don't provide any helpful information to say what that may be, but when Word 2010 creates documents it uses "AlternateContent" tags to provide different versions of the xml for different versions of Word and you may have to do the same, or limit yourself to Word 2007-compatible content.

How would I know what "Word 2007-compatible content" is in advance?

4

2 回答 2

0

此症状的最常见原因之一是文档包含 Office 14 命名空间中的元素/属性。

例如,您可能有这样的标记:

        <w:rPr>
      <w:color w:val="5B9BD5"
               w:themeColor="accent1"/>
      <w14:shadow w14:blurRad="38100"
                  w14:dist="25400"
                  w14:dir="5400000"
                  w14:sx="100000"
                  w14:sy="100000"
                  w14:kx="0"
                  w14:ky="0"
                  w14:algn="ctr">
        <w14:srgbClr w14:val="6E747A">
          <w14:alpha w14:val="57000"/>
        </w14:srgbClr>
      </w14:shadow>

您可能已经声明了 w14 命名空间,但根元素中还需要有一个属性:

mc:Ignorable="w14 w15 wp14"

您需要声明此属性:

<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:w15="http://schemas.microsoft.com/office/word/2012/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 w15 wp14">

这也意味着您需要专门使用这些命名空间前缀。

如果这不是问题,如果你愿意,我可以看看你的文件。我可能会很快告诉您为什么它不会在 2007 年开放。如果您在 OpenXMLDeveloper.org 的论坛上发帖,问题/示例文档会自动引起我的注意。

干杯,埃里克

于 2013-06-25T04:41:47.983 回答
0

在 Word 2007 中打开我的文档显示该行

<w:tblW w:w="auto" w:type="pct" />

引起了问题。使用 OpenXML SDK 工具进行验证,我发现了几个导致验证错误的错误:

  • 双打被用作 Int32
  • 元素的顺序很重要(根据架构这个问题
  • w:w="auto"不是有效的 Int32

也可以执行运行时验证,如此处所示

更改w:tblPr为:

<w:tblPr>
    <w:tblW w:type="auto" />
</w:tblPr>

修复验证错误后,文档仍然无法打开,这导致了tblW 标签

表格的首选宽度由<w:tblPr>元素中的<w:tblW>元素指定。表中的所有宽度都被认为是首选,因为列的宽度可能会发生冲突,并且表布局规则可能需要覆盖首选项。如果省略此元素,则单元格宽度为自动类型。

删除w:tblW元素解决了问题。

展望未来,似乎没有任何方法可以验证 OpenXml 以使其与 Word 2007 兼容。

于 2013-06-26T21:11:02.450 回答