René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Jasper - Subreports

First example

Ex_02_00.jrxml includes Ex_02_00_sub.jrxml. Both the driving report Ex_02_00 and the subreport Ex_02_00_sub consist of (or display) exactly one record.
Ex_02_00_sub.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport 
  xmlns="http://jasperreports.sourceforge.net/jasperreports" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
  name         ="report name" 
  pageWidth    =        "100" 
  pageHeight   =         "13"
  columnWidth  =        "100" 
  leftMargin   =          "0" 
  rightMargin  =          "0" 
  topMargin    =          "0" 
  bottomMargin =          "0"
>

	<queryString>
		<![CDATA[

  select 'From subreport' subreport
    from dual

--------------]]>

	</queryString>

	<field name="SUBREPORT"   class="java.lang.String"> <fieldDescription><![CDATA[]]></fieldDescription></field>

	<background>   <band            splitType="Stretch"/> </background>
	<title>        <band height="0" splitType="Stretch"/> </title>
	<pageHeader>   <band height="0" splitType="Stretch"/> </pageHeader>
	<columnHeader> <band height="0" splitType="Stretch"/> </columnHeader>

	<detail>
		<band height="13" splitType="Stretch">
			<textField> <reportElement x=  "0" y="0" width="100" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[$F{SUBREPORT}]]></textFieldExpression> </textField>
		</band>
	</detail>

	<columnFooter> <band height="0" splitType="Stretch"/> </columnFooter>
	<pageFooter>   <band height="0" splitType="Stretch"/> </pageFooter>
	<summary>      <band height="0" splitType="Stretch"/> </summary>

</jasperReport>
Ex_02_00.jrxml
<?xml version="1.0" encoding="UTF-8"?>


<!--

   Subreports

      first: compile Ex_02_00_sub.jrxml into 
                     Ex_02_00_sub.jasper.

      then: compile this file (Ex_02_00.jrxml) into
                     Ex_02_00.jasper

      then: run.


      additional notes: open source is when everybody claims everything is possible
                        but nobody knows how.

-->

<jasperReport 
  xmlns="http://jasperreports.sourceforge.net/jasperreports" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
  name              ="Demonstrating Subreports" 
  pageWidth         =        "595" 
  pageHeight        =        "845"
  columnWidth       =        "595" 
  leftMargin        =          "0" 
  rightMargin       =          "0" 
  topMargin         =          "0" 
  bottomMargin      =          "0"
>

	<queryString>
		<![CDATA[
    
  select 42 forty_two,
         99 ninety_nine
    from dual

--------------]]>
	</queryString>
	<field name="FORTY_TWO"   class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>
	<field name="NINETY_NINE" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>

	<title>        <band splitType="Stretch"/> </title>
	<pageHeader>   <band splitType="Stretch"/> </pageHeader>
	<columnHeader> <band splitType="Stretch"/> </columnHeader>

	<detail>
		<band height="13" splitType="Stretch">
			<textField> <reportElement x=  "0" y="0" width="100" height="13"/> <textElement/> <textFieldExpression class="java.lang.Float"><![CDATA[$F{FORTY_TWO}]]>  </textFieldExpression> </textField>
			<textField> <reportElement x="120" y="0" width="100" height="13"/> <textElement/> <textFieldExpression class="java.lang.Float"><![CDATA[$F{NINETY_NINE}]]></textFieldExpression> </textField>

			<subreport>
				<reportElement x="240" y="0" width="100" height="13"/>
				<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
				<subreportExpression class="java.lang.String"><![CDATA["Ex_02_00_sub.jasper"]]></subreportExpression>
			</subreport>
		</band>
	</detail>


	<columnFooter> <band splitType="Stretch"/> </columnFooter>
	<pageFooter>   <band splitType="Stretch"/> </pageFooter>
	<summary>      <band splitType="Stretch"/> </summary>
</jasperReport>

Second example

Demonstrates how to pass parameters from a driving report to a subreport. Similar to the first example, Ex_02_01.jrxml includes Ex_02_01_sub.jrxml.
Ex_02_01_sub expects two parameters, named O1 and O2 and constists of exactly one record whose values depend on the parameter values passed to the report.
Ex_02_01 consists of for records. The sub report is «called» for each of these records. The values of $F{VAL_1} and $F{VAL_2} is passed to the subreport within the <subreportParameter> directive.
Ex_02_01_sub.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport 
  xmlns="http://jasperreports.sourceforge.net/jasperreports" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
  name         ="report name" 
  pageWidth    =        "595" 
  pageHeight   =         "13"
  columnWidth  =        "100" 
  leftMargin   =          "0" 
  rightMargin  =          "0" 
  topMargin    =          "0" 
  bottomMargin =          "0"
>

<parameter name="O1" class="java.lang.Float" />
<parameter name="O2" class="java.lang.Float" />

	<queryString>
		<![CDATA[

  --------------

    select $P!{O1} + $P!{O2} a,
           $P!{O1} - $P!{O2} b,
           $P!{O1} * $P!{O2} c,
           $P!{O1} / $P!{O2} d
      from 
           dual

  --------------

   ]]>

	</queryString>

  <field name="A" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>
  <field name="B" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>
  <field name="C" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>
  <field name="D" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>

	<background>   <band            splitType="Stretch"/> </background>
	<title>        <band height="0" splitType="Stretch"/> </title>
	<pageHeader>   <band height="0" splitType="Stretch"/> </pageHeader>
	<columnHeader> <band height="0" splitType="Stretch"/> </columnHeader>

	<detail>
		<band height="13" splitType="Stretch">
      <textField> <reportElement x=  "0" y="0" width="150" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[""+$P{O1} + " + " + $P{O2} + " = " + $F{A} ]]> </textFieldExpression> </textField>
      <textField> <reportElement x="150" y="0" width="150" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[""+$P{O1} + " - " + $P{O2} + " = " + $F{B} ]]> </textFieldExpression> </textField>
      <textField> <reportElement x="300" y="0" width="150" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[""+$P{O1} + " * " + $P{O2} + " = " + $F{C} ]]> </textFieldExpression> </textField>
      <textField> <reportElement x="450" y="0" width="150" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[""+$P{O1} + " / " + $P{O2} + " = " + $F{D} ]]> </textFieldExpression> </textField>
		</band>
	</detail>

	<columnFooter> <band height="0" splitType="Stretch"/> </columnFooter>
	<pageFooter>   <band height="0" splitType="Stretch"/> </pageFooter>
	<summary>      <band height="0" splitType="Stretch"/> </summary>

</jasperReport>
Ex_02_01.jrxml
<?xml version="1.0" encoding="UTF-8"?>

<!--

   Subreports

      first: compile Ex_02_01_sub.jrxml into 
                     Ex_02_01_sub.jasper.

      then: compile this file (Ex_02_00.jrxml) into
                     Ex_02_01.jasper

      then: run.


      additional notes: open source is when everybody claims everything is possible
                        but nobody knows how.

-->

<jasperReport 
  xmlns="http://jasperreports.sourceforge.net/jasperreports" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
  name              ="Demonstrating Subreports" 
  pageWidth         =        "595" 
  pageHeight        =        "845"
  columnWidth       =        "595" 
  leftMargin        =          "0" 
  rightMargin       =          "0" 
  topMargin         =          "0" 
  bottomMargin      =          "0"
>

	<queryString>
		<![CDATA[
    
  select   0   val_1,   3    val_2 from dual union all
  select   0.5 val_1,   3.2  val_2 from dual union all
  select -11.8 val_1,   7.4  val_2 from dual union all
  select   2.6 val_1,  13.1  val_2 from dual

--------------]]>
	</queryString>
	<field name="VAL_1" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>
	<field name="VAL_2" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>

	<title>        <band splitType="Stretch"/> </title>
	<pageHeader>   <band splitType="Stretch"/> </pageHeader>
	<columnHeader> <band splitType="Stretch"/> </columnHeader>

	<detail>
		<band height="39" splitType="Stretch">
			<textField> <reportElement x=  "0" y="0" width="100" height="13"/> <textElement/> <textFieldExpression class="java.lang.Float"><![CDATA[$F{VAL_1}]]>  </textFieldExpression> </textField>
			<textField> <reportElement x="120" y="0" width="100" height="13"/> <textElement/> <textFieldExpression class="java.lang.Float"><![CDATA[$F{VAL_2}]]></textFieldExpression> </textField>

      <subreport isUsingCache="false">
        <reportElement positionType="Float" x="0" y="13" width="200" height="13" isRemoveLineWhenBlank="true"/>

        <subreportParameter name="O1"><subreportParameterExpression><![CDATA[$F{VAL_1}]]></subreportParameterExpression> </subreportParameter>
        <subreportParameter name="O2"><subreportParameterExpression><![CDATA[$F{VAL_2}]]></subreportParameterExpression> </subreportParameter>
        
				<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>

				<subreportExpression class="java.lang.String"><![CDATA["Ex_02_01_sub.jasper"]]></subreportExpression>
      
      </subreport>

		</band>
	</detail>


	<columnFooter> <band splitType="Stretch"/> </columnFooter>
	<pageFooter>   <band splitType="Stretch"/> </pageFooter>
	<summary>      <band splitType="Stretch"/> </summary>
</jasperReport>

Third example

The driving report places a subreport into the <pageHeader> section in order to demonstrate that by default a subreport is executed for each new page Header.
Ex_02_02_sub.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport 
  xmlns="http://jasperreports.sourceforge.net/jasperreports" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
  name         ="report name" 
  pageWidth    =        "100" 
  pageHeight   =         "13"
  columnWidth  =        "100" 
  leftMargin   =          "0" 
  rightMargin  =          "0" 
  topMargin    =          "0" 
  bottomMargin =          "0"
>

	<queryString>
		<![CDATA[

      --------------
        select dbms_random.value(0,100) rnd 
          from dual
      --------------
   ]]>
	</queryString>

	<field name="RND" class="java.math.BigDecimal"/>

	<detail>
		<band height="13" splitType="Stretch">
			<textField> <reportElement x=  "0" y="0" width="100" height="13"/> <textElement/> <textFieldExpression class="java.math.BigDecimal"><![CDATA[$F{RND}]]></textFieldExpression> </textField>
		</band>
	</detail>


</jasperReport>
Ex_02_02.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport 
  xmlns="http://jasperreports.sourceforge.net/jasperreports"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports
  http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
  name="Demonstrating Subreports"
  pageWidth="595" 
  pageHeight="845"
  columnWidth="595"
  leftMargin="0"
  rightMargin="0"
  topMargin="0"
  bottomMargin="0">
  
	<queryString>
		<![CDATA[with t as (
    select 0 d from dual union all
    select 1 d from dual
  )
  select 
    t7.d        d7,
    t6.d        d6,
    t5.d        d5,
    t4.d        d4,
    t3.d        d3,
    t2.d        d2,
    t1.d        d1,
    t0.d        d0,
    --
     1 * t0.d + 
     2 * t1.d +
     4 * t2.d +
     8 * t3.d +
    16 * t4.d +
    32 * t5.d +
    64 * t6.d +
   128 * t7.d    n
--
  from
    t t0, t t1, t t2, t t3, t t4, t t5, t t6, t t7


--------------]]>
	</queryString>
	<field name="D7" class="java.lang.Short"/>
	<field name="D6" class="java.lang.Short"/>
	<field name="D5" class="java.lang.Short"/>
	<field name="D4" class="java.lang.Short"/>
	<field name="D3" class="java.lang.Short"/>
	<field name="D2" class="java.lang.Short"/>
	<field name="D1" class="java.lang.Short"/>
	<field name="D0" class="java.lang.Short"/>
	<field name="N" class="java.lang.Short"/>

	<pageHeader>
		<band height="26" splitType="Stretch">
			<subreport isUsingCache="true">
				<reportElement x="0" y="0" width="100" height="13"/>
				<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
				<subreportExpression class="java.lang.String"><![CDATA["Ex_02_02_sub.jasper"]]></subreportExpression>
			</subreport>
		</band>
	</pageHeader>

	<columnHeader>
		<band splitType="Stretch"/>
	</columnHeader>
	<detail>
		<band height="13" splitType="Stretch">
			<textField>
				<reportElement x="0" y="0" width="10" height="13"/>
				<textElement/>
				<textFieldExpression class="java.lang.Short"><![CDATA[$F{D7}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="10" y="0" width="10" height="13"/>
				<textElement/>
				<textFieldExpression class="java.lang.Short"><![CDATA[$F{D6}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="20" y="0" width="10" height="13"/>
				<textElement/>
				<textFieldExpression class="java.lang.Short"><![CDATA[$F{D5}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="30" y="0" width="10" height="13"/>
				<textElement/>
				<textFieldExpression class="java.lang.Short"><![CDATA[$F{D4}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="40" y="0" width="10" height="13"/>
				<textElement/>
				<textFieldExpression class="java.lang.Short"><![CDATA[$F{D3}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="50" y="0" width="10" height="13"/>
				<textElement/>
				<textFieldExpression class="java.lang.Short"><![CDATA[$F{D2}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="60" y="0" width="10" height="13"/>
				<textElement/>
				<textFieldExpression class="java.lang.Short"><![CDATA[$F{D1}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="70" y="0" width="10" height="13"/>
				<textElement/>
				<textFieldExpression class="java.lang.Short"><![CDATA[$F{D0}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="100" y="0" width="30" height="13"/>
				<textElement/>
				<textFieldExpression class="java.lang.Short"><![CDATA[$F{N}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
</jasperReport>

Fourth example

This example demonstrats a subreport that returns multiple records rather than one.
Ex_02_03_sub.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport 
  xmlns="http://jasperreports.sourceforge.net/jasperreports" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
  name         ='report name' 
  pageWidth    =        '595' 
  pageHeight   =         '13'
  columnWidth  =        '100' 
  leftMargin   =          '0' 
  rightMargin  =          '0' 
  topMargin    =          '0' 
  bottomMargin =          '0'
>

	<queryString>
		<![CDATA[

    select 'but'            col_s from dual union all
    select 'these'          col_s from dual union all
    select 'come'           col_s from dual union all
    select 'from'           col_s from dual union all
    select 'the'            col_s from dual union all
    select 'subreport'      col_s from dual

   ]]>

	</queryString>

  <field name='COL_S' class="java.lang.String"/>


	<detail>
		<band height='13'>
      <textField> <reportElement x=  "0" y="0" width="150" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[$F{COL_S}]]> </textFieldExpression> </textField>
		</band>
	</detail>

</jasperReport>
Ex_02_03.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport
  xmlns="http://jasperreports.sourceforge.net/jasperreports"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports
  http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
  name="Demonstrating Subreports"
  pageWidth="595" 
  pageHeight="845" 
  columnWidth="595" 
  leftMargin="0"
  rightMargin="0"
  topMargin="0"
  bottomMargin="0">

	<queryString>
		<![CDATA[
       select   'these'      col_1 from dual union all
       select   'values'     col_1 from dual union all
       select   'are'        col_1 from dual union all
       select   'returned'   col_1 from dual union all
       select   'by'         col_1 from dual union all
       select   'the'        col_1 from dual union all
       select   'datasource' col_1 from dual union all
       select   'that'       col_1 from dual union all
       select   'is'         col_1 from dual union all
       select   'embedded'   col_1 from dual union all
       select   'in'         col_1 from dual union all
       select   'the'        col_1 from dual union all
       select   'driving'    col_1 from dual union all
       select   'report'     col_1 from dual]]>
	</queryString>
	<field name="COL_1" class="java.lang.String"/>
	<detail>
		<band height="61">
			<textField>
				<reportElement x="0" y="0" width="100" height="13"/>
				<textElement/>
				<textFieldExpression class="java.lang.String"><![CDATA[$F{COL_1}]]></textFieldExpression>
			</textField>

			<subreport>
				<reportElement x="240" y="0" width="100" height="31"/>
				<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
				<subreportExpression class="java.lang.String"><![CDATA["Ex_02_03_sub.jasper"]]></subreportExpression>
			</subreport>
		</band>

	</detail>
</jasperReport>

Fifth example

The driving report is not really a report as it has no meaningful query. It's used as a container for subreports. In order for the subreports to be shown, a dummy query is used.
Ex_02_04_sub.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport 
  xmlns="http://jasperreports.sourceforge.net/jasperreports" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
  name         ='report name' 
  pageWidth    =        '595' 
  pageHeight   =         '13'
  columnWidth  =        '100' 
  leftMargin   =          '0' 
  rightMargin  =          '0' 
  topMargin    =          '0' 
  bottomMargin =          '0'
>

	<queryString>
		<![CDATA[

    select '*ONE*'          col_s from dual union all
    select 'record'         col_s from dual union all
    select 'in'             col_s from dual union all
    select 'the'            col_s from dual union all
    select '''driving'''    col_s from dual union all
    select 'report'         col_s from dual union all
    select 'causes'         col_s from dual union all
    select '*ONE*'          col_s from dual union all
    select 'execution'      col_s from dual union all
    select 'of'             col_s from dual union all
    select 'the'            col_s from dual union all
    select 'subreport''s'   col_s from dual union all
    select 'query'          col_s from dual

   ]]>

	</queryString>

  <field name='COL_S' class="java.lang.String"/>


	<detail>
		<band height='13'>
      <textField> <reportElement x=  "0" y="0" width="150" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[$F{COL_S}]]> </textFieldExpression> </textField>
		</band>
	</detail>

</jasperReport>
Ex_02_04.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport 
  xmlns="http://jasperreports.sourceforge.net/jasperreports" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" 
  name         ='report name'
  pageWidth    =        '595'
  pageHeight   =        '200'
  columnWidth  =        '100'
  leftMargin   =          '0'
  rightMargin  =          '0'
  topMargin    =          '0'
  bottomMargin =          '0'
>

	<queryString>
		<![CDATA[
    select dummy from dual

   ]]>

	</queryString>

  <field name='DUMMY' class="java.lang.String"/>

	<detail>
		<band height="61" splitType="Stretch">
<!--  <textField> <reportElement x=  "0" y="0" width="150" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[$F{DUMMY}]]> </textFieldExpression> </textField> -->
			<subreport>
				<reportElement x="240" y="0" width="100" height="31"/>
				<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
				<subreportExpression class="java.lang.String"><![CDATA["Ex_02_04_sub.jasper"]]></subreportExpression>
			</subreport>
		</band>
	</detail>
</jasperReport>