我正在尝试使用以下代码创建Excel文件
XML:
<?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>
我有这个XSLT代码:
.exe
这导致了这一点,也是我需要的
任何人都可以帮我安排我的代码,以便它就像在图片中一样。
由于
答案 0 :(得分:1)
生成Excel XML的最佳方法是在Excel中进行所需的更改,保存,然后查看生成的XML。一旦你知道目标是什么,XSLT所需的变化通常会很难实现。
目前,您在XML中的每个Invoice
项目中执行一行,而实际上每个Items
元素需要一行。但是,需要考虑渲染行以获得预期的输出,因为每张发票的第一行将具有与发票的其他行不同的格式
Items
的第一个Invoice
的前四个单元格需要ss:MergeDown
属性,以便发票的字段向下跨越以覆盖所有项目行。Items
的其他Invoice
,他们需要从第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>