如何转换存储为文本的数字:使用 Jrxml /jasper

How to convert number stored as text : Using Jrxml /jasper

我使用 jrxml jasper 将我的内容导出到 .xls 文件。我有一个名为 PollutantQuantity 的文件,它作为字符串保存在数据库中。

我正在获取相同的值并将这些值提供给 .jrxml 。我可以看到正确填充的值没有任何问题。现在我的客户想直接从导出的 sheet .

中执行一些 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>

我正在使用 属性 字段,而且我的字段 emissionQty 被声明为字符串。我如何转换它以便在输出 excel 中 emissionQty 是 解释为数字。

将字段定义为 Number 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>

textFieldExpression

中的 if 语句

作为对建议解决方案的额外说明 - 如果您已经在处理数字字段(在我的例子中 java.math.BigDecimal)并且您无法在生成的 excel 中使用计算函数文件 (SUM) - 检查您的 JRXML 文件并查看它是否包含此 属性:

<property name="net.sf.jasperreports.export.xls.detect.cell.type" value="true"/>

OP 有 属性 但它不包含字符串中的 xls