我使用jrxml jasper将我的内容导出到.xls文件。我有一个名为PollutantQuantity
的文件,它作为字符串保留在DB中。
我正在获取相同的值,并将这些值提供给.jrxml。我可以看到这些值已正确填充,没有任何问题。现在,我的客户希望直接从导出的工作表中执行一些SUM
,MULTIPLY
功能。
在这种情况下,由于呈现为文本,因此我无法进行任何操作。
我的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>
我正在使用属性字段,并且我的字段missionQty也声明为字符串。我该如何转换,以便在输出excel中,missionQty为解释为数字。
将字段定义为Number es。 java.lang.Double
<field name="pollutantQty" class="java.lang.Double" />
在报告中使用pattern随意显示它
<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
来放置regex,以避免出现错误。
<printWhenExpression><![CDATA[$F{pollutantQty}.matches("-?\\d+(\\.\\d+)?")]]></printWhenExpression>
或者就好像textFieldExpression
中的语句
对于遇到相同问题的任何人,请注意-正确的属性名称应为:
<property name="net.sf.jasperreports.export.xls.detect.cell.type" value="true"/>
它必须在字符串中包含XLS。