[英]Place a custom table in word document programmatically usin openxml dll
[英]Programmatically save OpenXml document as previous version (Word 2007)
我有一些代碼可以創建要在MS Word中查看的文檔。 一些XML通過XSLT運行,然后添加到OpenXML文檔中。 生成的文檔在Word 2010中看起來不錯,但是在Word 2007中無法打開,我需要它能夠提供支持。 如何創建有效的Word 2007文檔?
使用Open XML Format SDK 2.0(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兼容的內容。
我怎么知道什么是“與Word 2007兼容的內容”?
出現此症狀的最常見原因之一是文檔包含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的論壇上發帖,問題/示例文檔將自動引起我的注意。
干杯,埃里克
在Word 2007中打開我的文檔顯示該行
<w:tblW w:w="auto" w:type="pct" />
造成了問題。 使用OpenXML SDK工具進行驗證,我發現了一些導致驗證錯誤的錯誤:
也可以如圖所示執行的運行時間驗證這里 。
將w:tblPr
更改為:
<w:tblPr>
<w:tblW w:type="auto" />
</w:tblPr>
修復驗證錯誤后,文檔仍然無法打開,這導致出現tblW標簽 :
該表的首選寬度由<w:tblPr>元素內的<w:tblW>元素指定。 表中的所有寬度均被視為首選寬度,因為列的寬度可能會發生沖突,並且表布局規則可能要求覆蓋首選項。 如果省略此元素,則像元寬度為auto類型。
刪除w:tblW
元素可以解決此問題。
展望未來,似乎沒有任何方法可以驗證OpenXml以使其與Word 2007兼容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.