格式化和组织电子表格结果

我正在尝试使用以下代码创buildExcel文件

XML:

<?xml version="1.0" encoding="utf-8"?> <Document> <Header> <NoInvoices>3</NoInvoices> <ExportDate>11-20-2015</ExportDate> </Header> <Invoices> <Type>CN</Type> <Customer>Juan</Customer> <CustAddress>New Bilibid</CustAddress> <InvNumber>01234</InvNumber> <Items> <Name>Sugar</Name> <Qty>2</Qty> <Price>5</Price> <Amount>10</Amount> <OrigInv>01235</OrigInv> </Items> </Invoices> <Invoices> <Type>SI</Type> <Customer>Juan</Customer> <CustAddress>New Bilibid</CustAddress> <InvNumber>01235</InvNumber> <Items> <Name>Coffee</Name> <Qty>5</Qty> <Price>25</Price> <Amount>125</Amount> </Items> <Items> <Name>Sugar</Name> <Qty>5</Qty> <Price>5</Price> <Amount>25</Amount> </Items> </Invoices> <Invoices> <Type>SI</Type> <Customer>Julianna</Customer> <CustAddress>New Wares</CustAddress> <InvNumber>01236</InvNumber> <Items> <Name>Margarine</Name> <Qty>1</Qty> <Price>50</Price> <Amount>50</Amount> </Items> <Items> <Name>Butter</Name> <Qty>10</Qty> <Price>10</Price> <Amount>100</Amount> </Items> </Invoices> </Document> 

我有这个XSLT代码:

 <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema"> <xsl:output method="xml" media-type="application/vnd.ms-excel" encoding="ISO-8859-1" indent="yes"/> <xsl:template match="/"> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" encoding="ISO-8859-1"> <xsl:call-template name="Styles"></xsl:call-template> <xsl:apply-templates select="Document" /> </Workbook> </xsl:template> <xsl:template name ="Styles"> <Styles > <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom"/> <Borders/> <Font/> <Interior/> <NumberFormat/> <Protection/> </Style> <Style ss:ID="s1"> <Alignment ss:Vertical="Center" ss:Horizontal="Center" ss:WrapText="1"/> <Borders> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#000000"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#000000"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#000000"/> </Borders> <Interior ss:Color="#FFB42A" ss:Pattern="Solid"/> <Font x:Family="Swiss" ss:Bold="1" /> </Style> </Styles> </xsl:template> <xsl:template match="Document"> <Worksheet> <xsl:attribute name="ss:Name">Invoices</xsl:attribute> <Table x:FullColumns="1" x:FullRows="1"> <xsl:apply-templates select="Invoices" /> </Table> </Worksheet> </xsl:template> <xsl:template match="Items"> <Cell> <Data ss:Type="String"> <xsl:value-of select ="Name"/> </Data> </Cell> <Cell> <Data ss:Type="Number"> <xsl:value-of select ="Qty"/> </Data> </Cell> <Cell> <Data ss:Type="Number"> <xsl:value-of select ="Price"/> </Data> </Cell> <Cell> <Data ss:Type="Number"> <xsl:value-of select ="Amount"/> </Data> </Cell> <Cell> <Data ss:Type="String"> <xsl:value-of select ="OrigInv"/> </Data> </Cell> </xsl:template> <xsl:template match="Invoices"> <Row> <Cell> <Data ss:Type="String"> <xsl:value-of select ="InvNumber"/> </Data> </Cell> <Cell> <Data ss:Type="String"> <xsl:value-of select ="Type"/> </Data> </Cell> <Cell> <Data ss:Type="String"> <xsl:value-of select ="Customer"/> </Data> </Cell> <Cell> <Data ss:Type="String"> <xsl:value-of select ="CustAddress"/> </Data> </Cell> <xsl:apply-templates select ="Items" /> </Row> </xsl:template> </xsl:stylesheet> 

这导致了这一点,也是我所需要的

在这里输入图像说明

任何人都可以帮我安排我的代码,以便它会像图片一样。

谢谢

生成Excel XML的最好方法是在Excel中进行所需的更改,保存,然后查看生成的XML。 一旦您知道要实现什么目标,XSLT所需的更改通常不会变得如此困难。

此刻,您正在为XML中的每个Invoice项目执行一行,确实需要每个Items元素有一行。 但是,需要考虑对行进行渲染以获得预期的输出,因为每张发票的第一行将与发票的其他行具有不同的格式

  1. 每个Invoice的第一个Items的前四个单元格将需要ss:MergeDown属性,以便发票的字段向下覆盖所有项目行。
  2. 对于每个Invoice的其他Items ,他们需要从第5列开始,因此需要ss:cellIndex来指定起始列

试试这个XSLT

 <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema"> <xsl:output method="xml" media-type="application/vnd.ms-excel" encoding="ISO-8859-1" indent="yes"/> <xsl:template match="/"> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40" encoding="ISO-8859-1"> <xsl:call-template name="Styles"></xsl:call-template> <xsl:apply-templates select="Document" /> </Workbook> </xsl:template> <xsl:template name ="Styles"> <Styles > <Style ss:ID="Default" ss:Name="Normal"> <Alignment ss:Vertical="Bottom"/> <Borders/> <Font/> <Interior/> <NumberFormat/> <Protection/> </Style> <Style ss:ID="s1"> <Alignment ss:Vertical="Center" ss:Horizontal="Center" ss:WrapText="1"/> <Borders> <Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#000000"/> <Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#000000"/> <Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1" ss:Color="#000000"/> </Borders> <Interior ss:Color="#FFB42A" ss:Pattern="Solid"/> <Font x:Family="Swiss" ss:Bold="1" /> </Style> </Styles> </xsl:template> <xsl:template match="Document"> <Worksheet> <xsl:attribute name="ss:Name">Invoices</xsl:attribute> <Table x:FullColumns="1" x:FullRows="1"> <xsl:apply-templates select="Invoices" /> </Table> </Worksheet> </xsl:template> <xsl:template match="Items"> <xsl:param name="cellIndex" select="1"></xsl:param> <Cell> <xsl:if test="$cellIndex > 1"> <xsl:attribute name="ss:Index"> <xsl:value-of select="$cellIndex" /> </xsl:attribute> </xsl:if> <Data ss:Type="String"> <xsl:value-of select ="Name"/> </Data> </Cell> <Cell> <Data ss:Type="Number"> <xsl:value-of select ="Qty"/> </Data> </Cell> <Cell> <Data ss:Type="Number"> <xsl:value-of select ="Price"/> </Data> </Cell> <Cell> <Data ss:Type="Number"> <xsl:value-of select ="Amount"/> </Data> </Cell> <Cell> <Data ss:Type="String"> <xsl:value-of select ="OrigInv"/> </Data> </Cell> </xsl:template> <xsl:template match="Invoices"> <xsl:variable name="merge" select="count(Items) - 1" /> <Row> <Cell ss:MergeDown="{$merge}"> <Data ss:Type="String"> <xsl:value-of select ="InvNumber"/> </Data> </Cell> <Cell ss:MergeDown="{$merge}"> <Data ss:Type="String"> <xsl:value-of select ="Type"/> </Data> </Cell> <Cell ss:MergeDown="{$merge}"> <Data ss:Type="String"> <xsl:value-of select ="Customer"/> </Data> </Cell> <Cell ss:MergeDown="{$merge}"> <Data ss:Type="String"> <xsl:value-of select ="CustAddress"/> </Data> </Cell> <xsl:apply-templates select="Items[1]" /> </Row> <xsl:for-each select="Items[position() > 1]"> <Row> <xsl:apply-templates select="."> <xsl:with-param name="cellIndex" select="5" /> </xsl:apply-templates> </Row> </xsl:for-each> </xsl:template> </xsl:stylesheet>