以编程方式将OpenXml文档另存为以前的版本(Word 2007) [英] Programmatically save OpenXml document as previous version (Word 2007)

查看:246
本文介绍了以编程方式将OpenXml文档另存为以前的版本(Word 2007)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以创建要在MS Word中查看的文档.一些XML通过XSLT运行,然后添加到OpenXML文档中.生成的文档在Word 2010中看起来不错,但是在Word 2007中无法打开,我需要它能够提供支持.如何创建有效的Word 2007文档?

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?

使用Open XML Format SDK 2.0(版本2.0.5022.0)

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();
}

.

Word 2010和Word 2007之间存在差异,它们遵循OOXML标准的不同版本.您很有可能包含了在Word 2010中可以使用的内容,但Word 2007无法处理.您没有提供任何有用的信息来说明可能是什么,但是在Word 2010创建文档时,它使用"AlternateContent"标签为不同版本的Word提供不同版本的xml,因此您可能必须执行相同操作或限制操作自己获得与Word 2007兼容的内容.

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.

我怎么会提前知道什么是与Word 2007兼容的内容"?

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

推荐答案

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

Opening my document in Word 2007 showed that the line

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

正在引起问题.使用OpenXML SDK工具进行验证,我发现了一些导致验证错误的错误:

was causing problems. Using the OpenXML SDK tool, for validation, I found several errors causing validation errors:

  • Doubles being used as Int32
  • Order of elements is important (per the schema and this question)
  • w:w="auto" is not a valid Int32

还可以按照此处所示执行运行时验证>.

Run time validation can also be performed as shown here.

w:tblPr更改为:

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

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

After fixing the validation errors, the document still wouldn't open, which led to the tblW tag:

表格的首选宽度由< w:tblPr> 元素内的< w:tblW> 元素指定.表中的所有宽度均被视为首选宽度,因为列的宽度可能会发生冲突,并且表布局规则可能会要求覆盖优先级.如果省略此元素,则像元宽度为自动类型.

The preferred width for the table is specified with the <w:tblW> element within the <w:tblPr> element. All widths in a table are considered preferred because widths of columns can conflict and the table layout rules can require a preference to be overridden. If this element is omitted, then the cell width is of type auto.

删除w:tblW元素可以解决此问题.

Removing the w:tblW element fixed the problem.

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

Going forward, it doesn't appear as if there is any way to validate an OpenXml such that it will be compatible with Word 2007.

这篇关于以编程方式将OpenXml文档另存为以前的版本(Word 2007)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆