只需对查询进行少量修改即可轻松完成,无需通过多种方式进行编程。
解决方案 1. 在 Detail 带中使用带有 Barcode 组件的单个报告
您可以使用单个报告的模板在一个报告中生成多个条形码。
在这种情况下,queryString 表达式(适用于 Oracle DB)将如下所示:
SELECT seq_barcode.nextval AS barcode, rownum FROM dual CONNECT BY LEVEL <= $P{quantity}
- 它根据您的需要多次从序列中生成一个值。 $P{quantity} 参数确定要生成的行数(条形码)。
工作 rjxml 文件:
<jasperReport ...>
<parameter name="quantity" class="java.lang.Integer">
<defaultValueExpression><![CDATA[20]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT seq_barcode.nextval AS barcode, rownum FROM dual CONNECT BY LEVEL <= $P{quantity}]]>
</queryString>
<field name="BARCODE" class="java.lang.Integer"/>
<field name="ROWNUM" class="java.lang.Integer"/>
<title>
<band height="82" splitType="Stretch">
<textField>
<reportElement x="145" y="18" width="240" height="20"/>
<textElement/>
<textFieldExpression><![CDATA["The number of barcodes is: " + $P{quantity}]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="47" splitType="Stretch">
<componentElement>
<reportElement x="145" y="10" width="200" height="28"/>
<jr:barbecue xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" type="2of7" drawText="false" checksumRequired="false">
<jr:codeExpression><![CDATA[$F{BARCODE}]]></jr:codeExpression>
</jr:barbecue>
</componentElement>
</band>
</detail>
</jasperReport>
结果将是 ($P{quantity} == 5):
在您的情况下,queryString 表达式将如下所示:
SELECT to_char(PALLET_ID_NO_SEQ.nextval) AS barcode, rownum FROM dual CONNECT BY LEVEL <= $P{quantity}
条码组件的表达式为:
new com.pepkorit.BarbecueRotateRenderer(
net.sourceforge.barbecue.BarcodeFactory.createCode128C($F{barcode}),
false, true, 1, 50, 190, 50)
解决方案 2. 使用组头带
您可以使用与第一个解决方案中相同的 queryString 表达式。 rownum 字段上的组将帮助我们生成 single 报告,其中包含许多属于其自己组的条形码(一组 - 一个条形码)。 Barcode 组件应放置在 Group Header 带区。
使用 isStartNewPage 属性,我们可以设法在新页面上生成组。
rjxml 文件:
<jasperReport ...>
<parameter name="quantity" class="java.lang.Integer">
<defaultValueExpression><![CDATA[20]]></defaultValueExpression>
</parameter>
<queryString>
<![CDATA[SELECT seq_barcode.nextval AS barcode, rownum FROM dual CONNECT BY LEVEL <= $P{quantity}]]>
</queryString>
<field name="BARCODE" class="java.lang.Integer"/>
<field name="ROWNUM" class="java.lang.Integer"/>
<group name="rownumGroup" isStartNewPage="true">
<groupExpression><![CDATA[$F{ROWNUM}]]></groupExpression>
<groupHeader>
<band height="50">
<componentElement>
<reportElement x="145" y="11" width="200" height="28"/>
<jr:barbecue xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" type="2of7" drawText="false" checksumRequired="false">
<jr:codeExpression><![CDATA[$F{BARCODE}]]></jr:codeExpression>
</jr:barbecue>
</componentElement>
</band>
</groupHeader>
</group>
<title>
<band height="82" splitType="Stretch">
<textField>
<reportElement x="145" y="18" width="240" height="20"/>
<textElement/>
<textFieldExpression><![CDATA["The number of barcodes is: " + $P{quantity}]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
如果 isStartNewPage="false" 组 rownumGroup 的结果将是 ($P{quantity} == 7):
如果 isStartNewPage="true" 组 rownumGroup 的结果将是 ($P{quantity} == 5):
解决方案 3. 使用子报表
我们可以将 Subreport 组件添加到 Detail 带(参见第一个解决方案)或 Group Header(见第二个解决方案)乐队。在这种情况下,您不仅可以向子报表添加 Barcode 组件,还可以添加您想要的所有内容。