OpenXml:工作表子元素的更改顺序导致文件损坏 [英] OpenXml: Worksheet Child Elements change in ordering results in a corrupt file
问题描述
我正在尝试使用openxml来生成自动化的excel文件.我面临的一个问题是使用excel的开放xml对象模型来容纳我的对象模型.我必须意识到我为工作表附加子元素的顺序很重要.
I am trying to use openxml to produce automated excel files. One problem I am facing is to accomodate my object model with open xml object model for excel. I have to come to a point where I realise that the order in which I append the child elements for a worksheet matters.
例如:
workSheet.Append(sheetViews);
workSheet.Append(columns);
workSheet.Append(sheetData);
workSheet.Append(mergeCells);
workSheet.Append(drawing);
上面的顺序没有给出任何错误.
the above ordering doesnot give any error.
但以下内容:
workSheet.Append(sheetViews);
workSheet.Append(columns);
workSheet.Append(sheetData);
workSheet.Append(drawing);
workSheet.Append(mergeCells);
出现错误
因此,这不允许我随时创建图形对象并将其附加到工作表中.迫使我在使用这些元素之前先创建它们.
So this doesn't let me to create a drawing object whenever I want to and append it to the worksheet. Which forces me to create these elements before using them.
有人可以告诉我我是否正确理解了这个问题?因为我相信我们应该能够打开任何Excel文件,所以在必要时为工作表创建一个新的子元素并将其附加.但这现在可能会破坏应该附加这些元素的顺序.
Can anyone tell me if I have understood the problem correctly ? Because I believe we should be able to open any excel file create a new child element for a worksheet if necessary and append it. But now this might break the order in which these elements are supposed to be appended.
谢谢.
推荐答案
According to the Standard ECMA-376 Office Open XML File Formats, CT_Worksheet
has a required sequence:
以下原因导致崩溃:
workSheet.Append(sheetViews);
workSheet.Append(columns);
workSheet.Append(sheetData);
workSheet.Append(drawing);
workSheet.Append(mergeCells);
是因为您拥有drawing
之前 mergeCells
.只要您在mergeCells
之后 drawing
后面附加代码,代码就可以正常工作.
Is because you have drawing
before mergeCells
. As long as you append your mergeCells
after drawing
, your code should work fine.
Note: You can find the full XSD in ECMA-376 3rd edition Part 1 (.zip) -> OfficeOpenXML-XMLSchema-Strict -> sml.xsd.
这篇关于OpenXml:工作表子元素的更改顺序导致文件损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!