Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a requirement to design in ireport.

I have three VOs

DeliveryAllocations {
    private String collectorCode;
    private String collectorName;
    private String month;
    private List<PlantVO> plants = new ArrayList<PlantVO>();

    with setter/getters
}
PlantVO{
     private String plantCode;
     private String plantName;
     private String TotalWeight;
     private List<PlantAllocationVO> allocations =  new ArrayList<PlantAllocationVO>();

      with setter/getters
}

PlantAllocationVO{
    private String weight;
    private String customerType;
    private String customerValue;
    private String comment;
    private String date;

    with setters/getters
}

Now I want to display two fields from PlantVO and three fields from PlantAllocationVO in a report with tabular format.

How do I achieve this? How do I fetch the data to design a report with this kind of structure?

tabular example

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
264 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is done by for example using a subreport and passing as datasource

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{allocations})

Example code:

Main report (PlantVO.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="PlantVO" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="55da4c97-0a7c-447f-b3a7-2713d483523d">
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\jdd\projects\StackTrace\jasper\"]]></defaultValueExpression>
    </parameter>
    <field name="plantName" class="java.lang.String"/>
    <field name="totalWeight" class="java.lang.Double"/>
    <field name="allocations" class="java.util.List"/>
    <detail>
        <band height="60" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="100" height="20" uuid="211879f7-331e-4526-bb0d-e7f0314f71b3"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA["Plant name: " + $V{REPORT_COUNT}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="100" y="0" width="200" height="20" uuid="f2d206d5-61fb-4238-93cf-c7dd16403a48"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{plantName}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement x="100" y="20" width="400" height="20" uuid="f3ca3eba-cb93-4ab4-8931-c399a8430841"/>
                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{allocations})]]></dataSourceExpression>
                <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "PlantAllocationVO_subreport.jasper"]]></subreportExpression>
            </subreport>
            <staticText>
                <reportElement positionType="Float" x="100" y="40" width="200" height="20" uuid="db7b40e6-37fe-4815-b912-fc5afaf966fb"/>
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <text><![CDATA[TOTAL WEIGHT:]]></text>
            </staticText>
            <textField pattern="###0">
                <reportElement positionType="Float" x="300" y="40" width="200" height="20" uuid="f1670bb4-efec-492c-b69c-0de668bda244"/>
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{totalWeight}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

Sub report (PlantAllocationVO_subreport)

<?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="PlantAllocationVO_subreport" pageWidth="400" pageHeight="802" columnWidth="400" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="417a7f13-6776-4773-94ab-0be5c01605c7">
    <field name="date" class="java.lang.String"/>
    <field name="weight" class="java.lang.Double"/>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="0" y="0" width="200" height="20" uuid="34424fa2-18d0-4859-825a-a07f2a826f55"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{date}]]></textFieldExpression>
            </textField>
            <textField pattern="###0">
                <reportElement x="200" y="0" width="200" height="20" uuid="a0e2ae10-906e-4d0f-aebd-30fc0c694aca"/>
                <textElement textAlignment="Right" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{weight}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

With main code

JasperReport report = JasperCompileManager.compileReport("PlantVO.jrxml");
Map<String, Object> map = new HashMap<String, Object>();
JasperPrint print = JasperFillManager.fillReport(report, map,new JRBeanCollectionDataSource(deliveryAllocations.getPlants()));
JasperExportManager.exportReportToPdfFile(print, "PlantVO.pdf");

Will produce this result:

Result

Some design notes:

  1. I would keep correct type of fields (es. weight as a Double or Integer) and apply pattern in jrxml (see example), this will help you do correct export to excel.

  2. TotalWeight there is no really need to have this as a field, it could be calculated by subreport and passed back to main report.

Have Fun


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...