我有2个子报告的报告,其中XML是: 根据这个问题Link to create subreport
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Report runDate="24/04/2014">
<TransactionTypeRowSet count="3">
<Row index="0">
<TransactionType>Funding</TransactionType>
<TransactionNo>50</TransactionNo>
<TotalAmount>55210</TotalAmount>
<TotalFee>530</TotalFee>
<TotalCommission>250</TotalCommission>
</Row>
<Row index="1">
<TransactionType>Person To Person</TransactionType>
<TransactionNo>75</TransactionNo>
<TotalAmount>78000</TotalAmount>
<TotalFee>780</TotalFee>
<TotalCommission>350</TotalCommission>
</Row>
<Row index="2">
<TransactionType>Load Float</TransactionType>
<TransactionNo>30</TransactionNo>
<TotalAmount>3000</TotalAmount>
<TotalFee>0</TotalFee>
<TotalCommission>0</TotalCommission>
</Row>
</TransactionTypeRowSet>
<TransactionRowSet count="2">
<Row index="0">
<TransactionId>9535789377931</TransactionId>
<Date>17-APR-2014</Date>
<Time>18:04:35</Time>
<TransactionType>Funding</TransactionType>
<TransactionStatus>Fund Success</TransactionStatus>
<TransactionAmount>100</TransactionAmount>
<TransactionFee>2</TransactionFee>
<TransactionCommission>1</TransactionCommission>
<WalletId>0003466476</WalletId>
<UserType>Service Provider</UserType>
<AccountType>Fee A/C</AccountType>
<MobileNo>0000000000</MobileNo>
<Amount>2</Amount>
<Type>C</Type>
</Row>
<Row index="1">
<TransactionId>9535789377931</TransactionId>
<Date>17-APR-2014</Date>
<Time>18:04:35</Time>
<TransactionType>Funding</TransactionType>
<TransactionStatus>Fund Success</TransactionStatus>
<TransactionAmount>100</TransactionAmount>
<TransactionFee>2</TransactionFee>
<TransactionCommission>1</TransactionCommission>
<WalletId>0007923578</WalletId>
<UserType>Service Provider</UserType>
<AccountType>Commission A/C</AccountType>
<MobileNo>0000000000</MobileNo>
<Amount>1</Amount>
<Type>C</Type>
</Row>
</TransactionRowSet>
</Report>
主报告的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="TransactionReport" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5f706135-d62b-4d94-a8ec-8d2f0d137aa9">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="Subreport_1" class="net.sf.jasperreports.engine.JasperReport"/>
<parameter name="Subreport_2" class="net.sf.jasperreports.engine.JasperReport"/>
<queryString language="xpath2">
<![CDATA[/Report]]>
</queryString>
<detail>
<band height="67" splitType="Stretch">
<subreport>
<reportElement x="0" y="0" width="187" height="67" uuid="325d0803-5a7b-4f06-a809-8c8a3e83bf8c"/>
<subreportParameter name="XML_DATA_DOCUMENT">
<subreportParameterExpression><![CDATA[$P{XML_DATA_DOCUMENT}]]></subreportParameterExpression>
</subreportParameter>
<subreportExpression><![CDATA[$P{Subreport_1}]]></subreportExpression>
</subreport>
</band>
<band height="66">
<subreport>
<reportElement x="0" y="0" width="197" height="66" uuid="3064bea5-2e7f-4cfc-ae02-3745703af1b0"/>
<subreportParameter name="XML_DATA_DOCUMENT">
<subreportParameterExpression><![CDATA[$P{XML_DATA_DOCUMENT}]]></subreportParameterExpression>
</subreportParameter>
<subreportExpression><![CDATA[$P{Subreport_2}]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
子报告1的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="TransactionReport_subreport1" pageWidth="802" pageHeight="555" orientation="Landscape" columnWidth="802" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="588e5172-b1db-40c6-927f-aca7a0b2876c">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="90"/>
<queryString language="xpath2">
<![CDATA[/Report/TransactionTypeRowSet/Row]]>
</queryString>
<field name="runDate" class="java.lang.String">
<fieldDescription><![CDATA[ancestor::Report/@runDate]]></fieldDescription>
</field>
<field name="TransactionType" class="java.lang.String">
<fieldDescription><![CDATA[TransactionType]]></fieldDescription>
</field>
<field name="TransactionNo" class="java.lang.String">
<fieldDescription><![CDATA[TransactionNo]]></fieldDescription>
</field>
<field name="TotalAmount" class="java.lang.String">
<fieldDescription><![CDATA[TotalAmount]]></fieldDescription>
</field>
<field name="TotalFee" class="java.lang.String">
<fieldDescription><![CDATA[TotalFee]]></fieldDescription>
</field>
<field name="TotalCommission" class="java.lang.String">
<fieldDescription><![CDATA[TotalCommission]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch">
<textField>
<reportElement x="128" y="0" width="100" height="20" uuid="aa03e739-6e8c-4566-99ea-2d15b27479f3"/>
<textFieldExpression><![CDATA[$F{runDate}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="10" y="0" width="45" height="20" uuid="59bf27b5-c260-4aa8-9347-f314619b73b4"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[Run Date]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="22" splitType="Stretch">
<staticText>
<reportElement x="10" y="2" width="100" height="20" uuid="a636f8fb-9dc5-4d5a-be72-faf2e569f1ce"/>
<text><![CDATA[Transaction Type]]></text>
</staticText>
<staticText>
<reportElement x="128" y="2" width="100" height="20" uuid="066d175f-783d-46a9-bb9b-15c6740aab94"/>
<text><![CDATA[Transaction No.]]></text>
</staticText>
<staticText>
<reportElement x="244" y="2" width="100" height="20" uuid="20d8dbc0-d94a-4aa2-8f20-c130d5d72937"/>
<text><![CDATA[Total Amount]]></text>
</staticText>
<staticText>
<reportElement x="358" y="2" width="100" height="20" uuid="1f47dc2d-5849-4440-8861-1523473b7ef7"/>
<text><![CDATA[Total Fee]]></text>
</staticText>
<staticText>
<reportElement x="470" y="2" width="100" height="20" uuid="48309245-2d2a-43b4-bc10-7f91e1e64612"/>
<text><![CDATA[Total Commission]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="10" y="0" width="100" height="20" uuid="44951d02-8cdb-4c98-b7d5-e58c6e46e807"/>
<textFieldExpression><![CDATA[$F{TransactionType}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="128" y="0" width="100" height="20" uuid="abaeec97-a833-4731-9272-4f52972a168a"/>
<textFieldExpression><![CDATA[$F{TransactionNo}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="244" y="0" width="100" height="20" uuid="ba6461a1-0042-49ce-8cc5-fef146fd2602"/>
<textFieldExpression><![CDATA[$F{TotalAmount}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="358" y="0" width="100" height="20" uuid="e28079e5-ad93-4c96-a030-de8da6d045e3"/>
<textFieldExpression><![CDATA[$F{TotalFee}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="470" y="0" width="100" height="20" uuid="4d9482db-e1dc-4e80-8547-87446e7bad0c"/>
<textFieldExpression><![CDATA[$F{TotalCommission}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
子报告2的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="TransactionReport_subreport2" pageWidth="1500" pageHeight="555" orientation="Landscape" columnWidth="1500" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="f22b3fa9-da70-48b3-a92b-7d9d5ce3c988">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="xpath2">
<![CDATA[/Report/TransactionRowSet/Row]]>
</queryString>
<field name="runDate" class="java.lang.String">
<fieldDescription><![CDATA[ancestor::Report/@runDate]]></fieldDescription>
</field>
<field name="TransactionId" class="java.lang.String">
<fieldDescription><![CDATA[TransactionId]]></fieldDescription>
</field>
<field name="Date" class="java.lang.String">
<fieldDescription><![CDATA[Date]]></fieldDescription>
</field>
<field name="Time" class="java.lang.String">
<fieldDescription><![CDATA[Time]]></fieldDescription>
</field>
<field name="TransactionType" class="java.lang.String">
<fieldDescription><![CDATA[TransactionType]]></fieldDescription>
</field>
<field name="TransactionStatus" class="java.lang.String">
<fieldDescription><![CDATA[TransactionStatus]]></fieldDescription>
</field>
<field name="TransactionAmount" class="java.lang.String">
<fieldDescription><![CDATA[TransactionAmount]]></fieldDescription>
</field>
<field name="TransactionFee" class="java.lang.String">
<fieldDescription><![CDATA[TransactionFee]]></fieldDescription>
</field>
<field name="TransactionCommission" class="java.lang.String">
<fieldDescription><![CDATA[TransactionCommission]]></fieldDescription>
</field>
<field name="WalletId" class="java.lang.String">
<fieldDescription><![CDATA[WalletId]]></fieldDescription>
</field>
<field name="UserType" class="java.lang.String">
<fieldDescription><![CDATA[UserType]]></fieldDescription>
</field>
<field name="AccountType" class="java.lang.String">
<fieldDescription><![CDATA[AccountType]]></fieldDescription>
</field>
<field name="MobileNo" class="java.lang.String">
<fieldDescription><![CDATA[MobileNo]]></fieldDescription>
</field>
<field name="Amount" class="java.lang.String">
<fieldDescription><![CDATA[Amount]]></fieldDescription>
</field>
<field name="Type" class="java.lang.String">
<fieldDescription><![CDATA[Type]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch">
<textField>
<reportElement x="110" y="0" width="100" height="20" uuid="4290617b-8c2a-4530-b0e5-106dd9c35135"/>
<textFieldExpression><![CDATA[$F{runDate}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="5" y="0" width="45" height="20" uuid="eb5b3fbe-99a8-4142-b70c-121250521ccd"/>
<textElement>
<font isBold="true"/>
</textElement>
<text><![CDATA[Run Date]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="22" splitType="Stretch">
<staticText>
<reportElement x="5" y="2" width="100" height="20" uuid="041974c0-9b9b-47a1-86b3-3dc3ce64f70f"/>
<text><![CDATA[Transaction Id]]></text>
</staticText>
<staticText>
<reportElement x="110" y="2" width="100" height="20" uuid="5c7f1106-d65e-4cca-9f82-92d7adb02a7d"/>
<text><![CDATA[Date]]></text>
</staticText>
<staticText>
<reportElement x="215" y="2" width="100" height="20" uuid="29432958-c1b2-4164-a6d2-61cda5131091"/>
<text><![CDATA[Time]]></text>
</staticText>
<staticText>
<reportElement x="320" y="2" width="100" height="20" uuid="b84c859b-44e2-4c58-ac50-30b6db62b752"/>
<text><![CDATA[Transaction Type]]></text>
</staticText>
<staticText>
<reportElement x="425" y="2" width="100" height="20" uuid="e77d633a-d760-4f5c-8d88-95fb91afab21"/>
<text><![CDATA[Transaction Status]]></text>
</staticText>
<staticText>
<reportElement x="530" y="2" width="100" height="20" uuid="0b4a24b8-2d64-42a2-9875-fd617ddf663e"/>
<text><![CDATA[Transaction Amount]]></text>
</staticText>
<staticText>
<reportElement x="634" y="2" width="100" height="20" uuid="c6610ab4-f4b0-445a-8f6c-d8e123ca9562"/>
<text><![CDATA[Fee]]></text>
</staticText>
<staticText>
<reportElement x="737" y="2" width="100" height="20" uuid="2446940d-ecda-47ff-90e1-1889898b8857"/>
<text><![CDATA[Commission]]></text>
</staticText>
<staticText>
<reportElement x="840" y="2" width="100" height="20" uuid="902717d0-bd96-44dd-9195-ceca8d2ca466"/>
<text><![CDATA[Wallet Id]]></text>
</staticText>
<staticText>
<reportElement x="943" y="2" width="100" height="20" uuid="246f09b5-f738-479b-819d-d8648f8e13a0"/>
<text><![CDATA[User Type]]></text>
</staticText>
<staticText>
<reportElement x="1047" y="2" width="100" height="20" uuid="68970e38-f49c-4a9b-8bf8-486298474fff"/>
<text><![CDATA[Account Type]]></text>
</staticText>
<staticText>
<reportElement x="1152" y="2" width="100" height="20" uuid="31714d60-87d2-41bd-bd74-60ea96d9adcf"/>
<text><![CDATA[MobileNo]]></text>
</staticText>
<staticText>
<reportElement x="1256" y="2" width="100" height="20" uuid="609bf515-758a-4cc6-b23a-ed603f036f12"/>
<text><![CDATA[Amount]]></text>
</staticText>
<staticText>
<reportElement x="1360" y="2" width="100" height="20" uuid="b8afcb53-8306-4569-8597-0f3213a09872"/>
<text><![CDATA[Type]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="5" y="0" width="100" height="20" uuid="29895b19-71b2-43ec-aa18-c5d23d3cece4"/>
<textFieldExpression><![CDATA[$F{TransactionId}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="110" y="0" width="100" height="20" uuid="1c42caa6-a106-45ff-96ec-65b4873c016d"/>
<textFieldExpression><![CDATA[$F{Date}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="215" y="0" width="100" height="20" uuid="6d85cc83-3830-44af-ac6d-517bb107d3ac"/>
<textFieldExpression><![CDATA[$F{Time}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="320" y="0" width="100" height="20" uuid="aee3e08d-744d-4578-89da-c5296237510e"/>
<textFieldExpression><![CDATA[$F{TransactionType}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="425" y="0" width="100" height="20" uuid="20ca50d3-c882-43f2-b3c0-334e7b7c3206"/>
<textFieldExpression><![CDATA[$F{TransactionStatus}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="530" y="0" width="100" height="20" uuid="3e0418dc-e327-4d94-ac4a-831e6cfdbcde"/>
<textFieldExpression><![CDATA[$F{TransactionAmount}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="634" y="0" width="100" height="20" uuid="ccec42b7-84e7-43ea-af56-3ee5af5b72cf"/>
<textFieldExpression><![CDATA[$F{TransactionFee}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="737" y="0" width="100" height="20" uuid="a18fc12e-467e-4ecc-b983-58d5eebb3bf3"/>
<textFieldExpression><![CDATA[$F{TransactionCommission}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="840" y="0" width="100" height="20" uuid="67a5658d-56ee-4f56-ac28-d5954febf852"/>
<textFieldExpression><![CDATA[$F{WalletId}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="943" y="0" width="100" height="20" uuid="9c032b11-4fc6-4b1b-89c4-83d508db9475"/>
<textFieldExpression><![CDATA[$F{UserType}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="1047" y="0" width="100" height="20" uuid="c8a4c818-6eb7-4186-b996-75b0fd9400b2"/>
<textFieldExpression><![CDATA[$F{AccountType}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="1152" y="0" width="100" height="20" uuid="dd8671c2-13d1-4b7a-829d-ded93701fc13"/>
<textFieldExpression><![CDATA[$F{MobileNo}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="1256" y="0" width="100" height="20" uuid="48fc8c4f-014d-493c-8b75-63b60dfc7ddc"/>
<textFieldExpression><![CDATA[$F{Amount}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="1360" y="0" width="100" height="20" uuid="006ea1f0-30a3-49b8-8c98-e5702146e9c6"/>
<textFieldExpression><![CDATA[$F{Type}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
现在,如何使用子报告为Jasper报告编写带有XML数据源的Java代码? 我试过这个:
JasperPrint print = new JasperPrint();
JRPdfExporter exporter = new JRPdfExporter();
JasperDesign design, design1;
JasperReport report, report1;
JasperReport jasperReport;
JasperDesign jd1;
jd1 = JRXmlLoader.load("C:/Users/bferrao/Desktop/jasper/TransactionReport_subreport1.jrxml");
jasperReport = JasperCompileManager.compileReport(jd1);
Map parameters = new HashMap();
parameters.put("Subreport_1", jasperReport);
design = JRXmlLoader.load("C:/Users/bferrao/Desktop/jasper/TransactionReport_subreport2.jrxml");
report = JasperCompileManager.compileReport(design);
parameters.put("Subreport_2", report);
Document document = JRXmlUtils.parse(new File("C:/Users/bferrao/Desktop/jasper/TransactionReport.xml"));
parameters.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document);
JRXmlDataSource jrxmlds = new JRXmlDataSource(new File("C:/Users/bferrao/Desktop/jasper/TransactionReport.xml"), "/Report");
design1 = JRXmlLoader.load("C:/Users/bferrao/Desktop/jasper/TransactionReport.jrxml");
report1 = JasperCompileManager.compileReport(design1);
JasperFillManager.fillReportToFile(report1, "C:/Users/bferrao/Desktop/jasper/master_report.jrprint", parameters, jrxmlds);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "D:\\temp\\Report3.pdf");
exporter.exportReport();
查看您的stackoverflow我已经修改了我的主报告和java构建报告。如何传递XML数据源?我还在报告空白。
答案 0 :(得分:0)
在Java代码中,您将两个subReport添加为参数Subreport_1
和Subreport_2
,但主报告仅声明一个名为“SUBREPORT_DIR”的参数。
我做了类似于Java代码的事情(总结):
// first compile the sub reports
jasperReport = JasperCompileManager.compileReport(jd1);
jasperReport = JasperCompileManager.compileReport(jd1);
report = JasperCompileManager.compileReport(design);
// then add the compiled reports to your parameters
Map parameters = new HashMap();
parameters.put("Subreport_1", jasperReport);
parameters.put("Subreport_2", report);
// and finally send the sub-reports to the main report as parameters
JasperFillManager.fillReportToFile(report1, "D:/temp/master_report.jrprint", parameters, jrxmlds);
您必须在master jrxml:
中将子报表声明为此参数<parameter name="Subreport_1" class="java.lang.Object"/>
<parameter name="Subreport_2" class="java.lang.Object"/>
...
<subreport runToBottom="false">
<reportElement uuid="ce3bf2a2-9131-4d75-800b-25d4a71791c3"
positionType="Float" isPrintRepeatedValues="false"
x="0" y="179" width="468" height="28" isRemoveLineWhenBlank="true"/>
<subreportParameter name="XML_DATA_DOCUMENT">
<subreportParameterExpression><![CDATA[$P{XML_DATA_DOCUMENT}]]></subreportParameterExpression>
</subreportParameter>
<subreportExpression><![CDATA[$P{Subreport_1}]]></subreportExpression>
</subreport>
并且可以删除以下参数,因为它似乎未在您的代码中使用:
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
<defaultValueExpression><![CDATA["C:\\Users\\bferrao\\Desktop\\jasper\\"]]></defaultValueExpression>
</parameter>
您必须要小心的另一件事是子报告中的数据源可见性。