问题
I am trying to create an xslt file that will take currency values in either EUR, JPY, GBP or USD and present them in an Excel output. To do this, I have created the following condition to capture the currency:
<Style ss:ID="s63">
<xsl:choose>
<xsl:when test='$currency = "EUR"'>
<NumberFormat ss:Format="_("€"\ * #,##0.00_);_("€"\ * \(#,##0.00\);_("€"\ * "-"??_);_(@_)"/>
</xsl:when>
<xsl:when test='$currency = "JPY"'>
<NumberFormat ss:Format="_("¥"\ * #,##0.00_);_("€"\ * \(#,##0.00\);_("€"\ * "-"??_);_(@_)"/>
</xsl:when>
<xsl:when test='$currency = "GBP"'>
<NumberFormat ss:Format="_-[$£-809]* #,##0.00_-;\-[$£-809]* #,##0.00_-;_-[$£-809]* "-"??_-;_-@_-"/>
</xsl:when>
<xsl:otherwise>
<NumberFormat ss:Format=""$"#,##0.00"/>
</xsl:otherwise>
</xsl:choose>
</Style>
This actually works quite well. As expected, I am getting the appropriate currency symbol. The problem I am having is with EUR. For some reason all of my currency values are getting multiplied by 100. I suspect it has something to do with the incoming format for EUR being in ###.###,00 format, going to #,##0.00 format. I can't get Excel to take ###.###,00 as a valid format, though. Does anyone know how I can keep my currency from multiplying? Thank you in advance!
回答1:
I figured this out. I basically need to treat the incoming values as a string since Excel doesn't like the #.###,00 format. So first I cast the value to a number in the correct format by:
<xsl:variable name="extCost" select="number(translate(extendedCost_line,'###.##0,00','###0.00'))"></xsl:variable>
Once I have my variable set, then I can format my number:
<Cell ss:StyleID="s63">
<Data ss:Type="Number"><xsl:value-of select="format-number(($extCost div 100), '#,##0.00', 'american')" /></Data>
</Cell>
This has forced me to create a row loop specific to EUR currency, and another row loop to capture all other currencies. This ends up giving me a number where the euro symbol appears before the number, but the formatting of the number is using a comma as the thousands separator and a period as a decimal point. The spreadsheet I'm creating is not customer-facing, so that is not an issue.
Thanks to those who commented! Hopefully this will help anyone facing the same challenge I was.
来源:https://stackoverflow.com/questions/22947831/foreign-currency-to-excel-xslt