2016-01-15 36 views
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> 

哪些结果这个,也是我需要的

enter image description here

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

感谢

回答

1

接近生成的Excel XML的最好办法,就是让你在Excel中所需的更改,保存它,然后看所生成的XML。一旦你知道瞄准什么,XSLT所需的改变通常不会那么困难。

目前您正在为您的XML中的每个Invoice项目做一行,当您真的想要每行Items一行时。然而,一些考虑需要给予行的渲染,以获得预期的输出,如第一行每个发票都会有不同的格式应用到其他行发票

  1. 前四个细胞的首先Items为每个Invoice将需要一个ss:MergeDown属性,以便发票的字段向下覆盖所有项目行。
  2. 对于其他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> 
+0

嗨很好的解决方案,但我有一个问题,将下面的代码处理项目超过2或不 1 ]“> ,因为通常发票超过2项 – zyberjock

+0

是的,它会处理两个以上。这个“if”语句只是测试是否有第二个项目,如果它存在,那么就为所有其他项目应用模板,而不是第二个项目。例如,如果有三项,if仍然返回true,并且都是好的。 –

+0

好的,因为我尝试过了,第三,没有被触发,并且出现错误,您能否尝试在您身边添加一个项目,感谢您的帮助 – zyberjock