Domanda

I need to design a report to represent a pojo structure like the following:

class Obj{
    field1;
    field2;

    List<Obj> substitutions;
}

My report will have the same field design, used one time in detail 1 band and then n-times in detail 2 with a subreport.

How can I reuse the same field design in both detail 1 and 2?

I have tryed to put a subreport in detail1 and a subreport in detail 2 pointing the same jrxml file. While the second report is ok because is filled by a collection in back object of report, I don't know how to pass current report datasource to first subreport.

Does someone have any ideas?

Here an example on the result report.

enter image description here

È stato utile?

Soluzione

I have a pojo class named purchase.

public class Purchase {
private String name;
private String remark;
 public void setName(String name) {
    this.name = name;
}
 public String getName() {
    return name;
}
public void setRemark(String remark) {
    this.remark = remark;
}
public String getRemark() {
    return remark;
}
}

Main class to generate pdf file.

public class JasperReportIntro {
public static void main(String[] args) {
    JasperReport jasperReport;
    JasperPrint jasperPrint;
    ArrayList<Purchase> list1 = new ArrayList<Purchase>();
    ArrayList<Purchase> list2 = new ArrayList<Purchase>();
    for(int i=0;i<20;i++) {
        Purchase purchase = new Purchase();
        purchase.setName("Vivek" + i);
        purchase.setRemark("This is remark" + i);
        purchase.setDiscount(10.0);
        purchase.setId(i);
        list1.add(purchase);
    }
    for(int i=0;i<20;i++) {
        Purchase purchase = new Purchase();
        purchase.setName("yadav" + i);
        purchase.setRemark("This is remark" + i);
        purchase.setDiscount(10.0);
        purchase.setId(i);
        list2.add(purchase);
    }

    try {
        jasperReport = JasperCompileManager.compileReport("path/to/report.jrxml");

        Map<String, List> result = new HashMap<String, List>();

        JRDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(list1);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dataForSubreport1",list1);
        map.put("dataForSubreport2",list2);
        jasperPrint = JasperFillManager.fillReport(jasperReport,map, beanCollectionDataSource);
        JasperExportManager.exportReportToPdfFile(jasperPrint,
                "path/to/simple_report.pdf");
        System.out.println("Completed");
    } catch (JRException e) {
        e.printStackTrace();
    }
}
}

Main report class 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="report" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4ddfa540-9b62-4339-9334-18733f7469cc">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA["/home/jft/code/jasper-report/Genratedreports/"]]></defaultValueExpression>
</parameter>
<parameter name="dataForSubreport1" class="java.util.List"/>
<parameter name="dataForSubreport2" class="java.util.List"/>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="100">
        <subreport>
            <reportElement x="0" y="20" width="555" height="40" uuid="8798ed7b-f389-4037-9381-3862d2f3e43a"/>
            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{dataForSubreport1})]]></dataSourceExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "sub1.jasper"]]></subreportExpression>
        </subreport>
        <subreport>
            <reportElement positionType="Float" x="0" y="60" width="555" height="40" uuid="f99fdb31-afc3-4127-8148-8e0426cd71a6"/>
            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{dataForSubreport2})]]></dataSourceExpression>
            <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "sub1.jasper"]]></subreportExpression>
        </subreport>
    </band>
</title>
</jasperReport>

Here is subreport sub1.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="sub1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="48bc0742-6980-42a7-9a52-6f125fb83bc2">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<field name="name" class="java.lang.String"/>
<background>
    <band splitType="Stretch"/>
</background>
<detail>
    <band height="20" splitType="Stretch">
        <textField>
            <reportElement x="0" y="0" width="100" height="20" uuid="ac3c1a86-6d0d-4b63-b339-744528573666"/>
            <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
        </textField>
    </band>
</detail>
</jasperReport>

Download file using main class of jasperreportintro. I have passed two parameter dataForSubreport1 and dataForSubreport2 using map. and send both parameter in subreport. It use same subreport with different -2 value. Hope it will help you.

Enjoy.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top