How to combine two already functioning reports?

大憨熊 提交于 2019-12-01 10:56:56

You have 2 options

1. Combine report creating a main report and include you reports in this as subreport's. You need to set margin to 0, whenNoDataType="AllSectionsNoDetail" and for example use the summary band to generate new page for report2 setting isSummaryNewPage="true". You do not need to change any queries, since you simple pass the report connection to your reports (subreports).

Example

<?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="test" pageWidth="612" pageHeight="792" whenNoDataType="AllSectionsNoDetail" columnWidth="612" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" isSummaryNewPage="true" uuid="9ac8b394-36b0-409a-8a94-b8147d9c2d20">
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\\jdd\\projects\\StackTrace\\jasper\\"]]></defaultValueExpression>
    </parameter>
    <title>
        <band height="20">
            <subreport>
                <reportElement x="0" y="0" width="612" height="20" uuid="e98a3620-58d6-47c1-8c93-6ca3d749b31b"/>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "report1.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </title>
    <summary>
        <band height="20">
            <subreport>
                <reportElement x="0" y="0" width="612" height="20" uuid="bc0c1758-9ce9-4f6d-a01c-2c77f59ae1fa"/>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "report2.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </summary>
</jasperReport>

2. Concatenate the report's during exporting

Example (pdf export is similar with other types of export)

Map<String, Object> paramMap = new HashMap<String, Object>();
List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
JasperPrint jasperPrint1 = JasperFillManager.fillReport(report1, paramMap);
jasperPrintList.add(jasperPrint1);
JasperPrint jasperPrint2 = JasperFillManager.fillReport(report2, paramMap);
jasperPrintList.add(jasperPrint2);

JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList)); //Set as export input my list with JasperPrint s
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("output.pdf")); //or any other out stream
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();
Teodor Danciu

There is now a third way to concatenate them, especially if the reports have different page size and/or orientation.

Instead of running them as subreports inside a master report, you can run them as parts inside a book report.

Please check the /demo/samples/book sample inside the JR Lib project distro package and you can see how 3 reports with different layouts are run together to produce a single file output.

Also, the /demo/samples/tableofcontents sample relies on this new type of report template, which we call book and is made of parts instead of bands.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!