Search code examples
jasper-reportssubreport

How to display each group of items on a new sheet in JasperReports?


I have a simple Spring-based web application for reporting about measurement tools. I'm using the following JRXML pattern (generated by TIBCO Jaspersoft Studio), let's say report_3.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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <style name="Default" isDefault="true" pdfFontName="ARIAL.TTF" pdfEncoding="Cp1251"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="polviz" class="java.lang.String"/>
    <field name="polzex" class="java.lang.String"/>
    <field name="polnaim" class="java.lang.String"/>
    <field name="tip" class="java.lang.String"/>
    <field name="klt" class="java.lang.Double"/>
    <field name="pred" class="java.lang.String"/>
    <field name="zavn" class="java.lang.String"/>
    <field name="npasp" class="java.lang.Integer"/>
    <field name="pp" class="java.lang.Integer"/>
    <field name="n" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="84">
            <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
            <staticText>
                <reportElement x="410" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[УТВЕРЖДАЮ]]></text>
            </staticText>
            <staticText>
                <reportElement x="410" y="14" width="100" height="20"  />
                <text><![CDATA[Главный метролог]]></text>
            </staticText>
            <line>
                <reportElement x="410" y="39" width="120" height="1"  />
            </line>
            <staticText>
                <reportElement x="410" y="39" width="140" height="30"  />
                <textElement>
                    <font size="6"/>
                </textElement>
                <text><![CDATA[(подпись, расшифровка подписи, дата)]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="85" splitType="Stretch">
            <staticText>
                <reportElement positionType="FixRelativeToBottom" x="-2" y="0" width="559" height="20"  />
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Times New Roman"/>
                </textElement>
                <text><![CDATA[Общая ведомость средств измерений по цехам]]></text>
            </staticText>
            <line>
                <reportElement x="0" y="83" width="554" height="1"  >
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
            </line>
            <textField>
                <reportElement stretchType="RelativeToTallestObject" x="0" y="66" width="554" height="16"  />
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{polviz}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="1" y="52" width="552" height="14"  />
                <textElement textAlignment="Right" verticalAlignment="Middle">
                    <font isItalic="false"/>
                </textElement>
                <text><![CDATA[/ N - не проверено, R - ремонт, К - консервация /]]></text>
            </staticText>
            <textField>
                <reportElement stretchType="RelativeToTallestObject" x="1" y="30" width="552" height="20"  />
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{polzex}]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="34" splitType="Stretch">
            <staticText>
                <reportElement x="1" y="1" width="110" height="30"  />
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Наим. средства изм.]]></text>
            </staticText>
            <staticText>
                <reportElement x="110" y="1" width="50" height="30"  />
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Тип]]></text>
            </staticText>
            <staticText>
                <reportElement x="160" y="1" width="68" height="30"  >
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Предел изм.]]></text>
            </staticText>
            <staticText>
                <reportElement x="280" y="1" width="78" height="30"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Зав. ном.]]></text>
            </staticText>
            <line>
                <reportElement x="0" y="32" width="554" height="1"  />
            </line>
            <line>
                <reportElement x="0" y="-2" width="1" height="34"  >
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
            </line>
            <line>
                <reportElement x="109" y="-2" width="1" height="34"  >
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
            </line>
            <line>
                <reportElement x="159" y="-2" width="1" height="34"  >
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                </reportElement>
            </line>
            <line>
                <reportElement x="228" y="-2" width="1" height="34"  />
            </line>
            <line>
                <reportElement x="359" y="-2" width="1" height="34"  />
            </line>
            <staticText>
                <reportElement x="360" y="1" width="74" height="30"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Ном. пасп.]]></text>
            </staticText>
            <line>
                <reportElement x="434" y="-2" width="1" height="34"  >
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                </reportElement>
            </line>
            <staticText>
                <reportElement x="436" y="0" width="54" height="30"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Периодич.]]></text>
            </staticText>
            <line>
                <reportElement x="490" y="-2" width="1" height="34"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                </reportElement>
            </line>
            <staticText>
                <reportElement x="490" y="0" width="62" height="30"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Неп,Рем,Конс]]></text>
            </staticText>
            <line>
                <reportElement x="554" y="-1" width="1" height="33"  >
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                </reportElement>
            </line>
            <staticText>
                <reportElement x="230" y="1" width="46" height="30"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[Кл.т.]]></text>
            </staticText>
            <line>
                <reportElement x="277" y="-2" width="1" height="34"  >
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                </reportElement>
            </line>
        </band>
    </columnHeader>
    <detail>
        <band height="33">
            <line>
                <reportElement x="1" y="19" width="553" height="1" forecolor="#E5E5E5"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
            </line>
            <textField>
                <reportElement x="2" y="1" width="108" height="18"  />
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{polnaim}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="111" y="1" width="50" height="18"  >
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{tip}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="161" y="1" width="68" height="18"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{pred}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="230" y="1" width="48" height="18"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{klt}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="278" y="1" width="82" height="18"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{zavn}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="360" y="1" width="76" height="18"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{npasp}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="436" y="1" width="56" height="18"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{pp}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="492" y="1" width="62" height="18"  >
                    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                </reportElement>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{n}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <lastPageFooter>
        <band height="21"/>
    </lastPageFooter>
</jasperReport>

Part of JasperReportsController controller:

...
@Controller
@RequestMapping("/jasper-reports/")
public class JasperReportsController {
    @Autowired(required = true)
    private MetrologServices metrologServices;

    @Autowired 
    private ApplicationContext appContext;

    @RequestMapping(method = RequestMethod.GET, value = "report_3")
    public ModelAndView generateThirdPdfReport(ModelAndView modelAndView) {
        JasperReportsPdfView jrPdfView = new JasperReportsPdfView();
        List<ReportDTO> records = findInventoryReportData();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("records", records);

        jrPdfView.setUrl("classpath:report_3.jrxml");
        jrPdfView.setReportDataKey("records");
        jrPdfView.setApplicationContext(appContext);

        modelAndView = new ModelAndView(jrPdfView, map);

        return modelAndView;
    }

    private List<ReportDTO> findInventoryReportData() {
        List<ReportDTO> result = metrologServices.loadInventoryData();
        return result;
    }

    ...
}

Somewhere in the view I call the controller:

$("#btn-print").attr("href", "${home}/metrolog/jasper-reports/report_3");

As a result I get the following report:

enter image description here

I want to start the list of measurement tools for each department with a new page. For example, part of the measuring tools belongs to department 1, another part belongs to department 2, etc.


Solution

  • I'd advise the following documentation: Chapter 14. View technologies, see section "14.7.4. Working with Sub-Reports":

    JasperReports provides support for embedded sub-reports within your master report files. There are a wide variety of mechanisms for including sub-reports in your report files. The easiest way is to hard code the report path and the SQL query for the sub report into your design files. The drawback of this approach is obvious - the values are hard-coded into your report files reducing reusability and making it harder to modify and update report designs. To overcome this you can configure sub-reports declaratively and you can include additional data for these sub-reports directly from your controllers.

    To control which sub-report files are included in a master report using Spring, your report file must be configured to accept sub-reports from an external source. To do this you declare a parameter in your report file like so:

    <parameter name="ProductsSubReport" class="net.sf.jasperreports.engine.JasperReport"/>
    

    Then, you define your sub-report to use this sub-report parameter:

    <subreport>
        <reportElement isPrintRepeatedValues="false" x="5" y="25" width="325"
            height="20" isRemoveLineWhenBlank="true" backcolor="#ffcc99"/>
        <subreportParameter name="City">
            <subreportParameterExpression><![CDATA[$F{city}]]></subreportParameterExpression>
        </subreportParameter>
        <dataSourceExpression><![CDATA[$P{SubReportData}]]></dataSourceExpression>
        <subreportExpression class="net.sf.jasperreports.engine.JasperReport">
                      <![CDATA[$P{ProductsSubReport}]]></subreportExpression>
    </subreport>
    

    This defines a master report file that expects the sub-report to be passed in as an instance of net.sf.jasperreports.engine.JasperReports under the parameter ProductsSubReport. When configuring your Jasper view class, you can instruct Spring to load a report file and pass into the JasperReports engine as a sub-report using the subReportUrls property:

    <property name="subReportUrls">
        <map>
            <entry key="ProductsSubReport" value="/WEB-INF/reports/subReportChild.jrxml"/>
        </map>
    </property>
    

    Here, the key of the Map corresponds to the name of the sub-report parameter in th report design file, and the entry is the URL of the report file. Spring will load this report file, compiling it if necessary, and will pass into the JasperReports engine under the given key.

    Hence, the master report file can be represented as follows:

    report_3.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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <style name="Default" isDefault="true" pdfFontName="ARIAL.TTF" pdfEncoding="Cp1251"/>
    
        <parameter name="Kpp9SubReport" class="net.sf.jasperreports.engine.JasperReport"/>
        <parameter name="Kpp9SubReportData" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    
        <parameter name="..." class="net.sf.jasperreports.engine.JasperReport"/>
        <parameter name="..." class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    
        ...
        etc
    
        <background>
            <band splitType="Stretch"/>
        </background>
        <title>
            <band height="84">
                <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                <staticText>
                    <reportElement x="410" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                    <textElement>
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[УТВЕРЖДАЮ]]></text>
                </staticText>
                <staticText>
                    <reportElement x="410" y="14" width="100" height="20"  />
                    <text><![CDATA[Главный метролог]]></text>
                </staticText>
                <line>
                    <reportElement x="410" y="39" width="120" height="1"  />
                </line>
                <staticText>
                    <reportElement x="410" y="39" width="140" height="30"  />
                    <textElement>
                        <font size="6"/>
                    </textElement>
                    <text><![CDATA[(подпись, расшифровка подписи, дата)]]></text>
                </staticText>
            </band>
        </title>
        <detail>
        <band height="500">
            <subreport>
                <reportElement isPrintRepeatedValues="false" x="-10" y="0" width="325" height="1" isRemoveLineWhenBlank="false" backcolor="#ffcc99"/>
                    <dataSourceExpression><![CDATA[$P{DepartmentKpp9SubReportData}]]></dataSourceExpression>
                    <subreportExpression class="net.sf.jasperreports.engine.JasperReport">
                                    <![CDATA[$P{DepartmentKpp9SubReport}]]></subreportExpression>
            </subreport>
            <break type="Page">
                <reportElement x="-10" y="1" width="325" height="10" key="element-1"/>
            </break>
        </band>
        <band height="500">
            <subreport> 
                <reportElement isPrintRepeatedValues="false" x="-10" y="2" width="325" height="1" isRemoveLineWhenBlank="false" backcolor="#ffcc99"/>
                    <dataSourceExpression><![CDATA[$P{Department8SubReportData}]]></dataSourceExpression>
                    <subreportExpression class="net.sf.jasperreports.engine.JasperReport">
                                    <![CDATA[$P{Department8SubReport}]]></subreportExpression>
            </subreport>
            <break type="Page">
                <reportElement x="-10" y="3" width="325" height="1" key="element-2"/>
            </break>
        </band>
        <band height="500">
           ...
        </band>
        ...
        etc
        </detail>
        <lastPageFooter>
            <band height="21"/>
        </lastPageFooter>
    </jasperReport>
    

    The sub-report report_4.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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <style name="Default" isDefault="true" pdfFontName="ARIAL.TTF" pdfEncoding="Cp1251"/>
        <field name="polviz" class="java.lang.String"/>
        <field name="polzex" class="java.lang.String"/>
        <field name="polnaim" class="java.lang.String"/>
        <field name="tip" class="java.lang.String"/>
        <field name="klt" class="java.lang.Double"/>
        <field name="pred" class="java.lang.String"/>
        <field name="zavn" class="java.lang.String"/>
        <field name="npasp" class="java.lang.Integer"/>
        <field name="pp" class="java.lang.Integer"/>
        <field name="n" class="java.lang.String"/>
        <background>
            <band splitType="Stretch"/>
        </background>
        <pageHeader>
            <band height="85" splitType="Stretch">
                <staticText>
                    <reportElement positionType="FixRelativeToBottom" x="-2" y="0" width="559" height="20"  />
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font fontName="Times New Roman"/>
                    </textElement>
                    <text><![CDATA[Общая ведомость средств измерений по цехам]]></text>
                </staticText>
                <line>
                    <reportElement x="0" y="83" width="554" height="1"  >
                        <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                    </reportElement>
                </line>
                <textField>
                    <reportElement stretchType="RelativeToTallestObject" x="0" y="66" width="554" height="16"  />
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{polviz}]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement x="1" y="52" width="552" height="14"  />
                    <textElement textAlignment="Right" verticalAlignment="Middle">
                        <font isItalic="false"/>
                    </textElement>
                    <text><![CDATA[/ N - не проверено, R - ремонт, К - консервация /]]></text>
                </staticText>
                <textField>
                    <reportElement stretchType="RelativeToTallestObject" x="1" y="30" width="552" height="20"  />
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$F{polzex}]]></textFieldExpression>
                </textField>
            </band>
        </pageHeader>
        <columnHeader>
            <band height="34" splitType="Stretch">
                <staticText>
                    <reportElement x="1" y="1" width="110" height="30"  />
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[Наим. средства изм.]]></text>
                </staticText>
                <staticText>
                    <reportElement x="110" y="1" width="50" height="30"  />
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[Тип]]></text>
                </staticText>
                <staticText>
                    <reportElement x="160" y="1" width="68" height="30"  >
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[Предел изм.]]></text>
                </staticText>
                <staticText>
                    <reportElement x="280" y="1" width="78" height="30"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[Зав. ном.]]></text>
                </staticText>
                <line>
                    <reportElement x="0" y="32" width="554" height="1"  />
                </line>
                <line>
                    <reportElement x="0" y="-2" width="1" height="34"  >
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    </reportElement>
                </line>
                <line>
                    <reportElement x="109" y="-2" width="1" height="34"  >
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    </reportElement>
                </line>
                <line>
                    <reportElement x="159" y="-2" width="1" height="34"  >
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                </line>
                <line>
                    <reportElement x="228" y="-2" width="1" height="34"  />
                </line>
                <line>
                    <reportElement x="359" y="-2" width="1" height="34"  />
                </line>
                <staticText>
                    <reportElement x="360" y="1" width="74" height="30"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[Ном. пасп.]]></text>
                </staticText>
                <line>
                    <reportElement x="434" y="-2" width="1" height="34"  >
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                </line>
                <staticText>
                    <reportElement x="436" y="0" width="54" height="30"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[Периодич.]]></text>
                </staticText>
                <line>
                    <reportElement x="490" y="-2" width="1" height="34"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                </line>
                <staticText>
                    <reportElement x="490" y="0" width="62" height="30"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[Неп,Рем,Конс]]></text>
                </staticText>
                <line>
                    <reportElement x="554" y="-1" width="1" height="33"  >
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                </line>
                <staticText>
                    <reportElement x="230" y="1" width="46" height="30"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle">
                        <font isBold="true"/>
                    </textElement>
                    <text><![CDATA[Кл.т.]]></text>
                </staticText>
                <line>
                    <reportElement x="277" y="-2" width="1" height="34"  >
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                </line>
            </band>
        </columnHeader>
        <detail>
            <band height="33">
                <line>
                    <reportElement x="1" y="19" width="553" height="1" forecolor="#E5E5E5"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    </reportElement>
                </line>
                <textField>
                    <reportElement x="2" y="1" width="108" height="18"  />
                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA[$F{polnaim}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="111" y="1" width="50" height="18"  >
                        <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA[$F{tip}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="161" y="1" width="68" height="18"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA[$F{pred}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="230" y="1" width="48" height="18"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA[$F{klt}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="278" y="1" width="82" height="18"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA[$F{zavn}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="360" y="1" width="76" height="18"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA[$F{npasp}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="436" y="1" width="56" height="18"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA[$F{pp}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="492" y="1" width="62" height="18"  >
                        <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA[$F{n}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
        <lastPageFooter>
            <band height="21"/>
        </lastPageFooter>
    </jasperReport>
    

    Part of JasperReportsController controller(we consider only the case of one department, the other sub-reports can be obtained similarly using the same templates report_3.jrxml and report_4.jrxml):

    ...
    @Controller
    @RequestMapping("/jasper-reports/")
    public class JasperReportsController {
        @Autowired(required = true)
        private MetrologServices metrologServices;
    
        @Autowired 
        private ApplicationContext appContext;
    
        @RequestMapping(method = RequestMethod.GET, value="report_3")
        public ModelAndView generateThirdPdfReport(ModelAndView modelAndView) {
            JasperReportsPdfView jrPdfView = new JasperReportsPdfView();
            List<ReportDTO> records = findInventoryReportData();
            Map<String, Object> map = new HashMap<>();
            map.put("records", records);
    
            Properties subReports = new Properties();
            subReports.put("Kpp9SubReport", "classpath:report_4.jrxml");
            List<ReportDTO> kpp9SubReportRecords = new ArrayList<>();
            records.forEach(record -> {
                if(record.getPolzex().equalsIgnoreCase("КПП 9")) {
                    kpp9SubReportRecords.add(record);
                }
            });
    
            JRDataSource kpp9SubReportDataSource = 
                new JRBeanCollectionDataSource(kpp9SubReportRecords);
            parameterMap.put("Kpp9SubReportData", kpp9SubReportDataSource);
    
            jrPdfView.setSubReportUrls(subReports);
            jrPdfView.setSubReportDataKeys("Kpp9SubReportData");
    
            jrPdfView.setUrl("classpath:report_3.jrxml");
            jrPdfView.setReportDataKey("records");
            jrPdfView.setApplicationContext(appContext);
    
            modelAndView = new ModelAndView(jrPdfView, map);
    
            return modelAndView;
        }
    
        private List<ReportDTO> findInventoryReportData() {
            List<ReportDTO> result = metrologServices.loadInventoryData();
            return result;
        }
    
        ...
    }
    

    As a result, we getting such a report, as required(in the department there are only five measuring instruments):

    enter image description here