0
我想创建一个Excel中使用下面的代码文件格式化和组织电子表格结果
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></InvNumber>
<Items>
<Name>Sugar</Name>
<Qty>2</Qty>
<Price>5</Price>
<Amount>10</Amount>
<OrigInv></OrigInv>
</Items>
</Invoices>
<Invoices>
<Type>SI</Type>
<Customer>Juan</Customer>
<CustAddress>New Bilibid</CustAddress>
<InvNumber></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></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>
哪些结果这个,也是我需要的
任何人都可以帮助我安排我的代码,以便它会像图片一样。
感谢
嗨很好的解决方案,但我有一个问题,将下面的代码处理项目超过2或不 1 ]“> xsl:apply-templates>
,因为通常发票超过2项 –
zyberjock
是的,它会处理两个以上。这个“if”语句只是测试是否有第二个项目,如果它存在,那么就为所有其他项目应用模板,而不是第二个项目。例如,如果有三项,if仍然返回true,并且都是好的。 –
好的,因为我尝试过了,第三,没有被触发,并且出现错误,您能否尝试在您身边添加一个项目,感谢您的帮助 –
zyberjock