2015-12-14 56 views
2

我使用jrxml jasper將我的內容導出到.xls文件。我有一個名爲PollutantQuantity的字段,它在數據庫中作爲字符串保存。如何轉換存儲爲文本的數字:使用Jrxml/jasper

我正在讀取相同的值,並將這些值賦給.jrxml。我可以看到正確填充的值沒有任何問題。現在我的客戶希望直接從導出的工作表中執行一些SUMMULTIPLY函數。

在這種情況下,由於被渲染爲文本,所以我不能做任何處理。

我JRXML代碼片段就像是

<property name="net.sf.jasperreports.export.detect.cell.type" value="true"/> 
<field name="pollutantQty" class="java.lang.String" /> 
<textField>  
    <reportElement x="0" y="0" width="100" height="20" isRemoveLineWhenBlank="true" /> 
<box leftPadding="10"><pen lineColor="#000000" /><topPen lineWidth="0.5" /><leftPen lineWidth="0.5" /><bottomPen lineWidth="0.5" /><rightPen lineWidth="0.5" /> 
    </box>          
    <textFieldExpression ><![CDATA[$F{pollutantQty}]]></textFieldExpression> 
</textField> 

我使用的屬性字段,也是我場emissionQty被聲明爲字符串。我怎樣才能轉換,以便在輸出excel的排放量是 解釋爲數字。

+0

嗨,大家好誰能請張貼如果u有任何解決方案 – user1912882

+0

皮特,下面的解決方案工作,謝謝你的分享 – user1912882

回答

0

將字段定義爲號碼 es。 java.lang.Double

<field name="pollutantQty" class="java.lang.Double" /> 

使用模式在報表中顯示它,只要你喜歡

<textField pattern='###,##0.00'>  
    <reportElement x="0" y="0" width="100" height="20" isRemoveLineWhenBlank="true" /> 
    <box leftPadding="10"><pen lineColor="#000000" /><topPen lineWidth="0.5" /><leftPen lineWidth="0.5" /><bottomPen lineWidth="0.5" /><rightPen lineWidth="0.5" /> 
    </box>         
    <textFieldExpression ><![CDATA[$F{pollutantQty}]]></textFieldExpression> 
</textField> 

如果你不能聲明爲一個號碼(其在數據庫中的字符串),你需要轉換字符串到數字es。

<textFieldExpression><![CDATA[Double.parseDouble($F{pollutantQty})]]></textFieldExpression> 

在這種情況下,它可能是明智的使用例如,以避免錯誤,把printWhenExpression

<printWhenExpression><![CDATA[$F{pollutantQty}.matches("-?\\d+(\\.\\d+)?")]]></printWhenExpression> 

或if語句中textFieldExpression

相關問題