JasperReports MasterReport-> Subreport-> Subreport

时间:2017-05-01 09:56:27

标签: java jasper-reports

任何人都可以举一个简单的例子来调用java子程序中子报表中子报表的jasper报表。这样的事情(Generate Jasper report with subreport from java):

JasperReport jasperMasterReport = JasperCompileManager.compileReport(masterReportSource);
JasperReport jasperSubReport1 = JasperCompileManager.compileReport(subReportSource1);
JasperReport jasperSubReport2 = JasperCompileManager.compileReport(subReportSource2);

Map<String, Object> parameters2 = new HashMap()<String, Object>;
parameters2.put("subreportParameter2", jasperSubReport2);

Map<String, Object> parameters1 = new HashMap()<String, Object>;
parameters1.put("subreportParameter1", JasperFillManager.fillReportToFile(jasperSubReport1, parameters2, connection));

JasperFillManager.fillReportToFile(jasperMasterReport, parameters1, connection);

MasterReport.jrxml

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-05-02T10:06:38 -->
<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="MasterReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5adfa1b1-651c-478e-a83b-6230d66dc454">
	<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
	<property name="com.jaspersoft.studio.data.defaultdataadapter" value="OracleJDBC"/>
    <parameter name="subreportParameter1" class="net.sf.jasperreports.engine.JasperReport"/>
	<queryString language="SQL">
		<![CDATA[SELECT "PERSON"."ID",
	"PERSON"."FIRST_NAME",
	"PERSON"."LAST_NAME"
FROM "PERSON"]]>
	</queryString>
	<field name="ID" class="java.math.BigDecimal"/>
	<field name="FIRST_NAME" class="java.lang.String"/>
	<field name="LAST_NAME" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<columnHeader>
		<band height="16" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="0" width="93" height="16" uuid="cde3f82c-d51e-43ef-a56e-fee3cb853b65"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[PERSONS:]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="80" splitType="Stretch">
			<textField>
				<reportElement x="0" y="20" width="30" height="20" uuid="9fbab9bb-55ec-44c0-badb-501a00334fe6"/>
				<textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement x="0" y="0" width="30" height="20" uuid="3c5afed0-442f-41de-9929-abdb8a218981"/>
				<text><![CDATA[ID]]></text>
			</staticText>
			<textField>
				<reportElement x="30" y="20" width="100" height="20" uuid="2561523d-322d-487d-89a2-be44804979ee"/>
				<textFieldExpression><![CDATA[$F{FIRST_NAME}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement x="30" y="0" width="100" height="20" uuid="431ac382-603e-47a1-b4df-b9d744041718"/>
				<text><![CDATA[FIRST NAME]]></text>
			</staticText>
			<textField>
				<reportElement x="130" y="20" width="100" height="20" uuid="9b482687-49c6-49f7-b322-ba868e4ce748"/>
				<textFieldExpression><![CDATA[$F{LAST_NAME}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement x="130" y="0" width="100" height="20" uuid="4532e6f3-58c0-4c24-aebc-a135309d94ba"/>
				<text><![CDATA[LAST NAME]]></text>
			</staticText>
			<subreport>
				<reportElement x="2" y="40" width="550" height="40" uuid="18ecc4c5-81a1-4ae3-b3f7-3bf10c627be8"/>
				<subreportParameter name="PERSON_ID">
					<subreportParameterExpression><![CDATA[$F{ID}]]></subreportParameterExpression>
				</subreportParameter>
				<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
				<subreportExpression><![CDATA[$P{subreportParameter1}]]></subreportExpression>
			</subreport>
		</band>
	</detail>
</jasperReport>
&#13;
&#13;
&#13;

Subreport1.jrxml

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-05-02T10:11:02 -->
<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="Subreport1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a0096e3a-50c2-49db-ba13-71afb020c425">
	<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
	<property name="com.jaspersoft.studio.data.defaultdataadapter" value="OracleJDBC"/>
	<parameter name="PERSON_ID" class="java.math.BigDecimal"/>
    <parameter name="subreportParameter2" class="net.sf.jasperreports.engine.JasperReport"/>
	<queryString language="SQL">
		<![CDATA[SELECT "PHONE"."ID",
	"PHONE"."PARENT_ID",
	"PHONE"."PHONE_NUMBER"
FROM "PHONE"
WHERE "PHONE"."PARENT_ID" = $P{PERSON_ID} ]]>
	</queryString>
	<field name="ID" class="java.math.BigDecimal"/>
	<field name="PARENT_ID" class="java.math.BigDecimal"/>
	<field name="PHONE_NUMBER" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<columnHeader>
		<band height="20" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="0" width="100" height="20" uuid="26c75f7b-f80c-41ca-a3fc-eb3317026efd"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[PHONES:]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="70" splitType="Stretch">
			<textField>
				<reportElement x="90" y="0" width="454" height="20" uuid="76669766-73b0-47f1-bfff-9337428a4776"/>
				<textFieldExpression><![CDATA[$F{PHONE_NUMBER}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement x="0" y="0" width="90" height="20" uuid="f925e71b-16fe-42ae-8895-143fb886eb63"/>
				<text><![CDATA[PHONE NUMBER:]]></text>
			</staticText>
			<subreport>
				<reportElement x="0" y="30" width="544" height="40" uuid="029f8258-5205-4e91-ae08-e8d8ec55f5e9"/>
				<subreportParameter name="PHONE_ID">
					<subreportParameterExpression><![CDATA[$F{ID}]]></subreportParameterExpression>
				</subreportParameter>
				<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
				<subreportExpression><![CDATA[$P{subreportParameter2}]]></subreportExpression>
			</subreport>
		</band>
	</detail>
</jasperReport>
&#13;
&#13;
&#13;

Subreport2.jrxml

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-05-02T10:13:15 -->
<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="Subreport2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9bd68d39-b94b-4e26-94ea-e0dc1e55f226">
	<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
	<property name="com.jaspersoft.studio.data.defaultdataadapter" value="OracleJDBC"/>
	<parameter name="PHONE_ID" class="java.math.BigDecimal"/>
	<queryString language="SQL">
		<![CDATA[SELECT "CONTACT_LOG"."ID",
	"CONTACT_LOG"."R_PHONE_ID",
	"CONTACT_LOG"."DSC"
FROM "CONTACT_LOG"
WHERE "CONTACT_LOG"."R_PHONE_ID" = $P{PHONE_ID}]]>
	</queryString>
	<field name="ID" class="java.math.BigDecimal"/>
	<field name="R_PHONE_ID" class="java.math.BigDecimal"/>
	<field name="DSC" class="java.lang.String"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<columnHeader>
		<band height="15" splitType="Stretch">
			<staticText>
				<reportElement x="0" y="0" width="100" height="15" uuid="0779105f-7c86-4541-906f-ea473cdc3ff5"/>
				<textElement>
					<font isBold="true"/>
				</textElement>
				<text><![CDATA[CONTACTS:]]></text>
			</staticText>
		</band>
	</columnHeader>
	<detail>
		<band height="30" splitType="Stretch">
			<textField>
				<reportElement x="102" y="0" width="448" height="30" uuid="67919643-f890-4121-ac18-59f87b0752f3"/>
				<textFieldExpression><![CDATA[$F{DSC}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement x="2" y="0" width="100" height="30" uuid="7c3824f0-41ff-4e53-8278-dcd0e535efd4"/>
				<text><![CDATA[COMMENT:]]></text>
			</staticText>
		</band>
	</detail>
</jasperReport>
&#13;
&#13;
&#13;

感谢。

1 个答案:

答案 0 :(得分:0)

您可以将子报表路径作为主报表参数传递,并获取此类子报表的数量。

            String file = /pathToMainReport/name.jrxml;
            String subreport1 = /pathToSubReport/name1.jasper;
            String subreport2 = /pathToSubReport/name2.jasper;      

            JasperReport jreport = JasperCompileManager.compileReport(file);


            Map<String, Object> map = new HashMap<String, Object>();

            map.put("SUBREPORT_DIR1", subreport1);
            map.put("SUBREPORT_DIR2", subreport2);

            JasperPrint jprint = JasperFillManager.fillReport(jreport, map, connection);