如何在 JasperReports 的新 sheet 上显示每组项目?

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

我有一个简单的基于 Spring 的 Web 应用程序,用于报告有关测量工具的信息。我正在使用以下 JRXML pattern (generated by TIBCO Jaspersoft Studio),假设 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>

部分JasperReportsController控制器:

...
@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;
    }

    ...
}

我调用控制器的视图中的某处:

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

结果我得到以下报告:

我想以新页面开始每个部门的测量工具列表。比如量具一部分属于部门1,另一部分属于部门2,等等

我建议使用以下文档:Chapter 14. View technologies,请参阅部分 “14.7.4。使用子报表”

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.

因此,主报告文件可以表示如下:

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>

子报表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>

部分JasperReportsController controller(我们只考虑一个部门的情况,其他子报表可以使用相同的模板report_3.jrxml和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;
    }

    ...
}

结果,我们按要求得到了这样一份报告(部门只有五台测量仪器):