如何在Table组件的帮助下显示JRBeanCollectionDataSource数据?

时间:2014-03-12 02:02:26

标签: java jasper-reports

我需要在Table组件(JasperReports)中显示JRBeanCollectionDataSource数据。

这是我的模板,ShowPerson.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="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>

我的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;
    }
}

构建报告的主要类:

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);
    }
}

2 个答案:

答案 0 :(得分:19)

1.从服务器发送数据源作为参数,并用另一个填充报告(可以为空)。

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.在JRBeanCollectionDataSource类型的报告中删除一个新参数

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

3.设置TableDatasource以使用$ P {DS1}参数。

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

4.Declare表数据集1中的字段(名称,年龄)。

5.在表格中,在DetailBand中,在每列中添加一个带有相应字段的TextField(分别为$ F {name}和$ F {age})。

答案 1 :(得分:3)

@laura:尝试将TableDatasource设置为使用$ P {DS1}参数时出现新错误。     net.sf.jasperreports.engine.JRBeanCollectionDataSource无法解析为类型

现在,我找到了解决方案,也许不是最好的解决方案。

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

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

将$ P {DS1}的类修改为java.util.Collection或java.util.List

并将表数据源设置为

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{DS1})