JasperReports collection as datasource?

2020-03-26 10:32发布

问题:

I'm trying to find out how to use a string collection as a datasource in JasperReports.

I guess I found the solution, but I can't get this to work, can anyone assist me?

  1. set as datasource the special data source JREmptyDatasource( $F{my_collection}.size() )
    This creates an empty data set with the same size of records as the collection.

  2. Pass the $F{my_collection} as parameter to the sub dataset (using the data set run of the List).

  3. To print the current element of the collection:

$P{my_collection_param}.get( $V{REPORT_COUNT} )

<?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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<subDataset name="dataset1">
    <parameter name="my_collection_param" class="java.util.Collection" isForPrompting="false">
        <defaultValueExpression><![CDATA[$P{my_collection}]]></defaultValueExpression>
    </parameter>
</subDataset>
<parameter name="my_collection" class="java.util.Collection">
    <defaultValueExpression><![CDATA["anna"]]></defaultValueExpression>
</parameter>
<field name="my_collection" class="java.util.Collection">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<background>
    <band splitType="Stretch"/>
</background>
<title>
    <band height="79" splitType="Stretch"/>
</title>
<pageHeader>
    <band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
    <band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
    <band height="125" splitType="Stretch">
        <componentElement>
            <reportElement x="51" y="30" width="400" height="50"/>
            <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                <datasetRun subDataset="dataset1">
                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource($F{my_collection}.size())]]></dataSourceExpression>
                </datasetRun>
                <jr:listContents height="50" width="0">
                    <textField>
                        <reportElement x="159" y="24" width="100" height="20"/>
                        <textElement/>
                        <textFieldExpression class="java.lang.String"><![CDATA[$P{my_collection_param}.get( $V{REPORT_COUNT} )]]></textFieldExpression>
                    </textField>
                </jr:listContents>
            </jr:list>
        </componentElement>
    </band>
</detail>
<columnFooter>
    <band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
    <band height="42" splitType="Stretch"/>
</summary>

回答1:

I guess your hack of using JREmptyDataSource is good by definition, if it works.

But you'll find that things are lot easier if you use this instead:

new net.sf.jasperreports.engine.data.JRMapCollectionDataSource($P{my_collection_param})