Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Excel OpenXml工作簿类中xmlelements的特定顺序_C#_Excel_Openxml - Fatal编程技术网

C# Excel OpenXml工作簿类中xmlelements的特定顺序

C# Excel OpenXml工作簿类中xmlelements的特定顺序,c#,excel,openxml,C#,Excel,Openxml,我通过OpenXml创建PowerPoint演示文稿。演示文稿的幻灯片中嵌入了丰富的对象。通常嵌入Excel文档。除了准备嵌入的图像和文档外,Excel还需要另一个信息,当在PowerPoint中打开文档时,应向用户显示该区域 通过向工作簿部分添加类,可以将此信息添加到Excel工作簿中。请忽略这段代码并没有检查该片段是否存在,这肯定会导致问题。这只是一个简单的例子 var oleSize = new OleSize() {Reference = "A1:H12"}; var w

我通过OpenXml创建PowerPoint演示文稿。演示文稿的幻灯片中嵌入了丰富的对象。通常嵌入Excel文档。除了准备嵌入的图像和文档外,Excel还需要另一个信息,当在PowerPoint中打开文档时,应向用户显示该区域

通过向工作簿部分添加类,可以将此信息添加到Excel工作簿中。请忽略这段代码并没有检查该片段是否存在,这肯定会导致问题。这只是一个简单的例子

    var oleSize = new OleSize() {Reference = "A1:H12"};
    var workbook = document.WorkbookPart.Workbook;
    workbook.Append(oleSize);
当刚刚添加到工作簿类的OpenXmlElements列表的末尾时,Excel将显示一个错误消息对话框,指出文件已损坏,无法修复。将此元素直接放在PivotCache OpenXmlElement之后可以作为一种解决方法。现在我体验到,当OpenXmlElement在OleSize之前可用时,Excel也会发出该消息

我找不到任何关于特定类的OpenXmlElements顺序的必要性的信息


非常感谢任何提示。

在深入研究OpenXml规范第4版后,我发现了以下信息:

sml_CT_Workbook =
     4261 attribute conformance { s_ST_ConformanceClass }?,
     4262 element fileVersion { sml_CT_FileVersion }?,
     4263 element fileSharing { sml_CT_FileSharing }?,
     4264 element workbookPr { sml_CT_WorkbookPr }?,
     4265 element workbookProtection { sml_CT_WorkbookProtection }?,
     4266 element bookViews { sml_CT_BookViews }?,
     4267 element sheets { sml_CT_Sheets },
     4268 element functionGroups { sml_CT_FunctionGroups }?,
     4269 element externalReferences { sml_CT_ExternalReferences }?,
     4270 element definedNames { sml_CT_DefinedNames }?,
     4271 element calcPr { sml_CT_CalcPr }?,
     4272 element oleSize { sml_CT_OleSize }?,
     4273 element customWorkbookViews { sml_CT_CustomWorkbookViews }?,
     4274 element pivotCaches { sml_CT_PivotCaches }?,
     4275 element smartTagPr { sml_CT_SmartTagPr }?,
     4276 element smartTagTypes { sml_CT_SmartTagTypes }?,
     4277 element webPublishing { sml_CT_WebPublishing }?,
     4278 element fileRecoveryPr { sml_CT_FileRecoveryPr }*,
     4279 element webPublishObjects { sml_CT_WebPublishObjects }?,
     4280 element extLst { sml_CT_ExtensionList }?
没有关于这是Garantued顺序还是预期顺序的信息,但仅通过几个测试,它看起来很有希望(我检查了其中一个测试工作簿中的工作簿openxml片段)


书$加元4
打印$A$1:$G$25

因此,我将尝试最佳猜测。

您在答案中列出的顺序是正确的。在您提到的OpenXML规范文档中,有一个XSD(第3936页)用于
工作簿
,它表明顺序确实是强制性的,因为它被定义为
序列

<xsd:complexType name="CT_Workbook">
    <xsd:sequence>
        <xsd:element name="fileVersion" type="CT_FileVersion" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="fileSharing" type="CT_FileSharing" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="workbookPr" type="CT_WorkbookPr" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="workbookProtection" type="CT_WorkbookProtection" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="bookViews" type="CT_BookViews" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="sheets" type="CT_Sheets" minOccurs="1" maxOccurs="1"/>
        <xsd:element name="functionGroups" type="CT_FunctionGroups" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="externalReferences" type="CT_ExternalReferences" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="definedNames" type="CT_DefinedNames" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="calcPr" type="CT_CalcPr" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="oleSize" type="CT_OleSize" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="customWorkbookViews" type="CT_CustomWorkbookViews" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="pivotCaches" type="CT_PivotCaches" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="smartTagPr" type="CT_SmartTagPr" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="smartTagTypes" type="CT_SmartTagTypes" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="webPublishing" type="CT_WebPublishing" minOccurs="4129 0" maxOccurs="1"/>
        <xsd:element name="fileRecoveryPr" type="CT_FileRecoveryPr" minOccurs="0" maxOccurs="unbounded"/>
        <xsd:element name="webPublishObjects" type="CT_WebPublishObjects" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="extLst" type="CT_ExtensionList" minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="conformance" type="s:ST_ConformanceClass"/>
</xsd:complexType>


如果您感兴趣,我有一个答案,其中显示了将项目添加到文件中正确位置的一种方法。

正是我所寻找的。谢谢事实上,我不明白为什么需要强制订单,但我想他们有很好的理由:-)实际上,您的实现中有一个小故障。查找前一个元素时,for each需要一个break语句:
openxmlement-precedingelement=null
foreach(工作表part.Worksheet.ChildElements.Reverse()中的变量项)
{/code>
if(elements.Contains(item.GetType())
{/code>
中断;
]
}
谢谢@HolgerLeichsenring-我已经解决了另一个问题上的代码示例,因为你说得很对。我也觉得奇怪的是,顺序很重要,但也许它可以在阅读时带来一些优化?
<xsd:complexType name="CT_Workbook">
    <xsd:sequence>
        <xsd:element name="fileVersion" type="CT_FileVersion" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="fileSharing" type="CT_FileSharing" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="workbookPr" type="CT_WorkbookPr" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="workbookProtection" type="CT_WorkbookProtection" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="bookViews" type="CT_BookViews" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="sheets" type="CT_Sheets" minOccurs="1" maxOccurs="1"/>
        <xsd:element name="functionGroups" type="CT_FunctionGroups" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="externalReferences" type="CT_ExternalReferences" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="definedNames" type="CT_DefinedNames" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="calcPr" type="CT_CalcPr" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="oleSize" type="CT_OleSize" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="customWorkbookViews" type="CT_CustomWorkbookViews" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="pivotCaches" type="CT_PivotCaches" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="smartTagPr" type="CT_SmartTagPr" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="smartTagTypes" type="CT_SmartTagTypes" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="webPublishing" type="CT_WebPublishing" minOccurs="4129 0" maxOccurs="1"/>
        <xsd:element name="fileRecoveryPr" type="CT_FileRecoveryPr" minOccurs="0" maxOccurs="unbounded"/>
        <xsd:element name="webPublishObjects" type="CT_WebPublishObjects" minOccurs="0" maxOccurs="1"/>
        <xsd:element name="extLst" type="CT_ExtensionList" minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="conformance" type="s:ST_ConformanceClass"/>
</xsd:complexType>