Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show JRBeanCollectionDataSource data with help of Table component?

I need to show JRBeanCollectionDataSource data in Table component (JasperReports).

Here is my template, ShowPerson.jrxml file:

<?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="ShowPerson" pageWidth="612" pageHeight="792" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="304c4c4e-c99a-4399-8081-748d3b7c0b8c">
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Table Dataset 1" whenResourceMissingType="Empty" uuid="63b01547-bce2-47c9-ba15-666f94d11387">
        <queryString language="SQL">
            <![CDATA[]]>
        </queryString>
        <field name="name" class="java.lang.String"/>
        <field name="age" class="java.lang.Integer"/>
    </subDataset>
    <parameter name="INFO" class="java.lang.String"/>
    <title>
        <band height="40" splitType="Stretch">
            <staticText>
                <reportElement uuid="e96450a8-8358-4cae-a094-3add06d57de2" x="0" y="20" width="56" height="20"/>
                <textElement/>
                <text><![CDATA[Info]]></text>
            </staticText>
            <textField>
                <reportElement uuid="e21e9932-ebfe-4bb5-904d-ea99e141866b" x="56" y="20" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$P{INFO}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <detail>
        <band height="40" splitType="Stretch">
            <componentElement>
                <reportElement uuid="dea5d821-81b6-434b-ae27-4f3a0268e805" key="table 1" x="0" y="0" width="572" height="40"/>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="Table Dataset 1" uuid="39f8f0bf-8a2b-44f4-9a4c-0c873da79fba">
                        <datasetParameter name="REPORT_DATA_SOURCE">
                            <datasetParameterExpression><![CDATA[]]></datasetParameterExpression>
                        </datasetParameter>
                        <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="169" uuid="aae649c4-6a69-485f-8a0d-87022df793ee">
                        <jr:tableHeader height="20" rowSpan="1">
                            <staticText>
                                <reportElement uuid="795dac43-0ff0-482c-89a0-7dac3b27d513" x="0" y="0" width="169" height="20"/>
                                <textElement/>
                                <text><![CDATA[Name]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell height="20" rowSpan="1">
                            <textField>
                                <reportElement uuid="4f1ab13a-d776-4cd5-a2a7-a5cf23360816" x="0" y="0" width="169" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="173" uuid="a6997eea-131e-4555-80e6-a20d4decb18f">
                        <jr:tableHeader height="20" rowSpan="1">
                            <staticText>
                                <reportElement uuid="5f6e2413-8025-47ca-9638-9609ea13f93f" x="0" y="0" width="173" height="20"/>
                                <textElement/>
                                <text><![CDATA[Age]]></text>
                            </staticText>
                        </jr:tableHeader>
                        <jr:detailCell height="20" rowSpan="1">
                            <textField>
                                <reportElement uuid="195d51a0-9e45-4201-ad67-d3026ce2e72c" x="0" y="0" width="173" height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

My POJO:

public class Person {

    private String name;
    private int age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

Main class for building report:

public class OpenReport {
    public static void main(String[] args) throws JRException {
        Map<String, Object> peopleMap = new HashMap<>();
        peopleMap.put("Sisco", 17);
        peopleMap.put("Eve", 19);
        peopleMap.put("John", 20);
        peopleMap.put("George", 21);
        peopleMap.put("Steve", 18);

        ArrayList<Person> dataList = new ArrayList<Person>();

        for(Map.Entry<String, Object> personMap : peopleMap.entrySet()) {
            Person person = new Person();
            person.setName(personMap.getKey());
            person.setAge(Integer.valueOf(personMap.getValue().toString()));
            dataList.add(person);
        }

        JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
        Map parameters = new HashMap();
        parameters.put("INFO", "Hello");

        JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, beanColDataSource);

        JFrame frame = new JFrame("Report");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new JRViewer(jasperPrint));
        frame.pack();
        frame.setVisible(true);
    }
}
like image 359
Scooby Avatar asked Mar 12 '14 02:03

Scooby


People also ask

How do I use JRBeanCollectionDataSource in Jasper report?

In JRBeanCollectionDataSource(listjrbean) you should add collection of objects/beans. You need to verify bean should have getter and setter for empCode. For Ex class EmpClass { private String empCode; private String empName; public String getEmpCode() { return empCode; } public void setEmpCode(String empCode) { this.

What is JRBeanCollectionDataSource?

A data source implementation that wraps a collection of JavaBean objects. It is common to access application data through object persistence layers like EJB, Hibernate, or JDO. Such applications may need to generate reports using data they already have available as arrays or collections of in-memory JavaBean objects.


1 Answers

1.Send your datasource from the server as a parameter, and fill the report with a different one (can be empty).

JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
        Map parameters = new HashMap();
        parameters.put("INFO", "Hello");
        parameters.put("DS1", beanColDataSource);

        JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
        JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());

2.Decalre a new parameter in the report of type JRBeanCollectionDataSource

<parameter name="DS1" class="net.sf.jasperreports.engine.JRBeanCollectionDataSource"/>

3.Set TableDatasource to use the $P{DS1} parameter.

<jr:table ...>
    <datasetRun subDataset="Table Dataset 1">
        <datasetParameter name="REPORT_DATA_SOURCE">
             <datasetParameterExpression><![CDATA[$P{DS1}]]></datasetParameterExpression>
        </datasetParameter>
    </datasetRun>
.....

4.Declare the fields (name, age) in Table Dataset 1.

5.In the table, in the DetailBand add a TextField in each column with the corresponding field ($F{name} and $F{age} respectively).

like image 103
Laura Avatar answered Oct 24 '22 19:10

Laura