serialNo
これは、文字列ではなく何らかの数値であると仮定して、それを行う1つの方法です。
- 上に示したように、SQL クエリ結果のデータセットは で並べ替える
serialNo
必要があります。
Report Group
次のグループ式で を作成します: $V{GroupCount} == new BigDecimal(0) ? $F{serialNo}:$F{serialNo}.subtract($V{GroupCount})
. serialNo
これにより、シーケンスにギャップがあるたびに新しいグループが作成されます。
- 現在のグループ内
GroupCount
の連続する値の数をカウントする変数を作成します。この変数は、次に期待される値serialNo
を追跡するために使用されます。serialNo
StartRange
連続する値の特定の範囲の開始値を保持する変数を作成しserialNo
ます。値は、新しいグループの開始時にリセットされ、次の値を持つ必要があります: $V{GroupCount}.intValue() == 1 ? $F{ID}:$V{StartRange}
.
- フィールドの
Evaluation Time
値を に設定します。次の値を入れます。serialNo
ReportGroup
$V{StartRange}+" - " + $F{serialNo}
- バンドの を次のように
Print When Expression
変更します。Detail
new Boolean($V{GroupCount}.intValue() == 1)
以下に完全な jrxml コードを掲載しました。
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with iReport - A designer for JasperReports -->
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="Group_By_Consecutive" columnCount="1" printOrder="Vertical" orientation="Portrait" pageWidth="595" pageHeight="842" columnWidth="535" columnSpacing="0" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20" whenNoDataType="NoPages" isTitleNewPage="false" isSummaryNewPage="false">
<property name="ireport.scriptlethandling" value="0" />
<property name="ireport.encoding" value="UTF-8" />
<import value="java.util.*" />
<import value="net.sf.jasperreports.engine.*" />
<import value="net.sf.jasperreports.engine.data.*" />
<queryString>
<![CDATA[select 1 as id, 'Name 1' as name from dual union all
select 2 as id, 'Name 2' as name from dual union all
select 3 as id, 'Name 3' as name from dual union all
select 4 as id, 'Name 4' as name from dual union all
select 6 as id, 'Name 6' as name from dual union all
select 7 as id, 'Name 7' as name from dual union all
select 8 as id, 'Name 8' as name from dual union all
select 9 as id, 'Name 9' as name from dual union all
select 10 as id, 'Name 10' as name from dual union all
select 11 as id, 'Name 11' as name from dual union all
select 14 as id, 'Name 14' as name from dual union all
select 15 as id, 'Name 15' as name from dual union all
select 16 as id, 'Name 16' as name from dual union all
select 17 as id, 'Name 17' as name from dual union all
select 23 as id, 'Name 23' as name from dual union all
select 24 as id, 'Name 24' as name from dual union all
select 25 as id, 'Name 25' as name from dual union all
select 26 as id, 'Name 26' as name from dual union all
select 27 as id, 'Name 27' as name from dual union all
select 28 as id, 'Name 28' as name from dual]]>
</queryString>
<field name="ID" class="java.math.BigDecimal" />
<field name="NAME" class="java.lang.String" />
<variable name="GroupCount" class="java.math.BigDecimal" resetType="Group" resetGroup="Consecutive" calculation="Count">
<variableExpression>
<![CDATA[$F{ID}]]>
</variableExpression>
<initialValueExpression>
<![CDATA[new BigDecimal(0)]]>
</initialValueExpression>
</variable>
<variable name="StartRange" class="java.math.BigDecimal" resetType="Report" calculation="Nothing">
<variableExpression>
<![CDATA[$V{GroupCount}.intValue() == 1 ? $F{ID}:$V{StartRange}]]>
</variableExpression>
</variable>
<group name="Consecutive">
<groupExpression>
<![CDATA[$V{GroupCount} == new BigDecimal(0) ? $F{ID}:$F{ID}.subtract($V{GroupCount})]]>
</groupExpression>
<groupHeader>
<band height="0" isSplitAllowed="true"></band>
</groupHeader>
<groupFooter>
<band height="0" isSplitAllowed="true"></band>
</groupFooter>
</group>
<background>
<band height="0" isSplitAllowed="true"></band>
</background>
<title>
<band height="0" isSplitAllowed="true"></band>
</title>
<pageHeader>
<band height="0" isSplitAllowed="true"></band>
</pageHeader>
<columnHeader>
<band height="18" isSplitAllowed="true">
<staticText>
<reportElement x="0" y="0" width="200" height="18" key="staticText-1" />
<box></box>
<textElement>
<font />
</textElement>
<text>
<![CDATA[serialNo]]>
</text>
</staticText>
<staticText>
<reportElement x="200" y="0" width="100" height="18" key="staticText-3" />
<box></box>
<textElement>
<font />
</textElement>
<text>
<![CDATA[Name]]>
</text>
</staticText>
<staticText>
<reportElement x="300" y="0" width="100" height="18" key="staticText-4" />
<box></box>
<textElement>
<font />
</textElement>
<text>
<![CDATA[MetaData(Ignore)]]>
</text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="18" isSplitAllowed="true">
<printWhenExpression>
<![CDATA[new Boolean($V{GroupCount}.intValue() == 1)]]>
</printWhenExpression>
<textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self">
<reportElement x="200" y="0" width="100" height="18" key="textField" />
<box></box>
<textElement>
<font />
</textElement>
<textFieldExpression class="java.lang.String">
<![CDATA[$F{NAME}]]>
</textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" pattern="##0.00" isBlankWhenNull="false" evaluationTime="Group" evaluationGroup="Consecutive" hyperlinkType="None" hyperlinkTarget="Self">
<reportElement x="0" y="0" width="200" height="18" key="textField" />
<box></box>
<textElement>
<font />
</textElement>
<textFieldExpression class="java.lang.String">
<![CDATA[$V{StartRange}+" - " + $F{ID}]]>
</textFieldExpression>
</textField>
<textField isStretchWithOverflow="false" pattern="##0.00" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self">
<reportElement x="300" y="0" width="100" height="18" key="textField" />
<box></box>
<textElement>
<font />
</textElement>
<textFieldExpression class="java.math.BigDecimal">
<![CDATA[$V{GroupCount}]]>
</textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="0" isSplitAllowed="true"></band>
</columnFooter>
<pageFooter>
<band height="0" isSplitAllowed="true"></band>
</pageFooter>
<summary>
<band height="0" isSplitAllowed="true"></band>
</summary>
</jasperReport>