Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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
如何使用XML模式将多个Excel行导出到XML文件?_Xml_Excel_Excel 2010_Xsd.exe - Fatal编程技术网

如何使用XML模式将多个Excel行导出到XML文件?

如何使用XML模式将多个Excel行导出到XML文件?,xml,excel,excel-2010,xsd.exe,Xml,Excel,Excel 2010,Xsd.exe,我有一个Excel工作表,其中有一个汽车列表: 我创建了一个XML模式文档,以便将工作表导出为XML。模式如下: <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="Root"> <xsd:complexType>

我有一个Excel工作表,其中有一个汽车列表:

我创建了一个XML模式文档,以便将工作表导出为XML。模式如下:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

在Excel中,我单击开发人员>源>XML映射>添加,然后从上面选择XML模式文件并将其添加到我的工作表中。 我将XML源窗口中的每个元素映射到Excel工作表中的相应列(“Brand”元素映射到A:A,“Model”元素映射到B:B,等等)

在导出映射的工作表时(使用Developer>Export,然后选择目录和文件名),生成的XML文件只有工作表中的第一行数据。由于我的XML映射包含工作表的标题行,XML文件如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Brand>Brand</Brand>
    <Model>Model</Model>
    <Colour>Colour</Colour>
    <Price>Price</Price>
</Root>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Car_Table">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Root" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>  
    </xsd:element>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

烙印
模型
颜色
价格

我已经浏览了许多不同的帮助页面,我不知道如何使我的XML文件存储Excel工作表的每一行(全部五行,包括标题行)。

您应该添加以下内容:

minOccurs="0" maxOccurs="unbounded"
告诉excel您的元素可以出现多次

但不能将其添加到“Root”元素中

试试这个:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Car" minOccurs="0" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="Brand" type="xsd:string"/>
                            <xsd:element name="Model" type="xsd:string"/>
                            <xsd:element name="Colour" type="xsd:string"/>
                            <xsd:element name="Price" type="xsd:string"/> 
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

您应该添加以下内容:

minOccurs="0" maxOccurs="unbounded"
告诉excel您的元素可以出现多次

但不能将其添加到“Root”元素中

试试这个:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Car" minOccurs="0" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="Brand" type="xsd:string"/>
                            <xsd:element name="Model" type="xsd:string"/>
                            <xsd:element name="Colour" type="xsd:string"/>
                            <xsd:element name="Price" type="xsd:string"/> 
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

我没有使用J.Doe的答案,但我认为它是正确的,因为它与我最终所做的类似。链接原始XML架构时:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Excel假定工作表中只有一条记录。解决方案是在我的模式中创建第二个复杂元素(除了名为“Root”的元素之外),它包含>1个我的根元素。生成的文件如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Brand>Brand</Brand>
    <Model>Model</Model>
    <Colour>Colour</Colour>
    <Price>Price</Price>
</Root>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Car_Table">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Root" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>  
    </xsd:element>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

使用Developer>Source>XMLMaps>Add添加模式时,会出现如下提示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Brand>Brand</Brand>
    <Model>Model</Model>
    <Colour>Colour</Colour>
    <Price>Price</Price>
</Root>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Car_Table">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Root" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>  
    </xsd:element>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

选择引用其他元素的元素(因为这允许递归)。在“XML源”面板中,映射将如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Brand>Brand</Brand>
    <Model>Model</Model>
    <Colour>Colour</Colour>
    <Price>Price</Price>
</Root>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Car_Table">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Root" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>  
    </xsd:element>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

然后,您可以将模式中的每个元素(品牌、型号、颜色、价格)映射到工作表中的相应列。架构映射到工作表后,数据将如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Brand>Brand</Brand>
    <Model>Model</Model>
    <Colour>Colour</Colour>
    <Price>Price</Price>
</Root>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Car_Table">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Root" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>  
    </xsd:element>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

然后,您可以通过转到Developer>export将数据导出为XML。使用“导出”对话框选择文件名和目录后,生成的XML文件应如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Brand>Brand</Brand>
    <Model>Model</Model>
    <Colour>Colour</Colour>
    <Price>Price</Price>
</Root>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Car_Table">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Root" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>  
    </xsd:element>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>


这是使用Excel 2010完成的,其他版本会有所不同。

我没有使用J.Doe的答案,但我认为它是正确的,因为它与我最终所做的类似。链接原始XML架构时:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Excel假定工作表中只有一条记录。解决方案是在我的模式中创建第二个复杂元素(除了名为“Root”的元素之外),它包含>1个我的根元素。生成的文件如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Brand>Brand</Brand>
    <Model>Model</Model>
    <Colour>Colour</Colour>
    <Price>Price</Price>
</Root>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Car_Table">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Root" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>  
    </xsd:element>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

使用Developer>Source>XMLMaps>Add添加模式时,会出现如下提示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Brand>Brand</Brand>
    <Model>Model</Model>
    <Colour>Colour</Colour>
    <Price>Price</Price>
</Root>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Car_Table">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Root" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>  
    </xsd:element>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

选择引用其他元素的元素(因为这允许递归)。在“XML源”面板中,映射将如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Brand>Brand</Brand>
    <Model>Model</Model>
    <Colour>Colour</Colour>
    <Price>Price</Price>
</Root>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Car_Table">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Root" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>  
    </xsd:element>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

然后,您可以将模式中的每个元素(品牌、型号、颜色、价格)映射到工作表中的相应列。架构映射到工作表后,数据将如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Brand>Brand</Brand>
    <Model>Model</Model>
    <Colour>Colour</Colour>
    <Price>Price</Price>
</Root>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Car_Table">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Root" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>  
    </xsd:element>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

然后,您可以通过转到Developer>export将数据导出为XML。使用“导出”对话框选择文件名和目录后,生成的XML文件应如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
    <Brand>Brand</Brand>
    <Model>Model</Model>
    <Colour>Colour</Colour>
    <Price>Price</Price>
</Root>
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Car_Table">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="Root" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>  
    </xsd:element>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>   
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>


这是使用Excel 2010完成的,其他版本会有所不同。

对我来说,只需编辑模式,并为每个实体创建多个条目即可。因此,在你的情况下:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                 <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>  
                <xsd:element name="Price" type="xsd:string"/> 
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>


只需要为每个实体输入两个条目,这样就可以导出多个条目。

对我有效的方法是编辑模式并为每个实体输入多个条目。因此,在你的情况下:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <!--Below are the primary vehicle descriptors - essentially the attributes for the cars-->
                <xsd:element name="Brand" type="xsd:string"/>
                 <xsd:element name="Brand" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Model" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Colour" type="xsd:string"/>
                <xsd:element name="Price" type="xsd:string"/>  
                <xsd:element name="Price" type="xsd:string"/> 
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>