只需对查询进行少量修改即可轻松完成,而无需通过多种方式进行编程。
解决方案 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}
Barcode组件的表达式为:
new com.pepkorit.BarbecueRotateRenderer(
net.sourceforge.barbecue.BarcodeFactory.createCode128C($F{barcode}),
false, true, 1, 50, 190, 50)
解决方案 2. 使用组头带
您可以使用与第一个解决方案中相同的queryString表达式。rownum字段上的组将帮助我们生成单个报告,其中包含许多属于其自己组的条形码(一组 - 一个条形码)。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>
如果组rownumGroup的isStartNewPage="false"结果将是($P{quantity}== 7):

如果组rownumGroup的isStartNewPage="true" ,结果将是($P{quantity} == 5):

解决方案 3. 使用子报表
我们可以将Subreport组件添加到Detail带区(参见第一个解决方案)或Group Header(参见第二个解决方案)带区。在这种情况下,您不仅可以向子报表添加Barcode组件,还可以添加您想要的所有内容。