1

当我尝试使用JavaBean 数据源生成报告并在表中显示 bean 的字段值时,PDF报告显示空值。

我有一个简单的 bean(PersonBean),如下所示:

public class PersonBean {

    private String Field1;
    private String Field2;

    public String getField1() {
        return Field1;
    }

    public void setField1(String field1) {
        Field1 = field1;
    }

    public String getField2() {
        return Field2;
    }

    public void setField2(String field2) {
        Field2 = field2;
    }
}

填充并返回 bean 集合的Person类如下:

import java.util.ArrayList;
import java.util.List;

public class Person {

    public static java.util.List<PersonBean> getReportData() throws Exception {
        List<PersonBean> personBeanList = null;
        try {
            personBeanList = new ArrayList<PersonBean>();

            PersonBean personBean1 = new PersonBean();
            personBean1.setField1("Hina");
            personBean1.setField2("Sachdev");
            personBeanList.add(personBean1);

            PersonBean personBean2 = new PersonBean();
            personBean2.setField1("Swathi");
            personBean2.setField2("Singh");
            personBeanList.add(personBean2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return personBeanList;
    }
}

创建报表的PersonReport类如下:

import java.io.FileNotFoundException;
import java.util.*;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.view.JasperViewer;

public class PersonReport implements JRDataSource {

    public static void main(String[] args) throws JRException, FileNotFoundException {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            JasperDesign jasperDesign = JRXmlLoader.load("C:/Users/sachdevh/Desktop/reports/personReport.jrxml");
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, 
                    new JRBeanCollectionDataSource(Person.getReportData()));
            JasperExportManager.exportReportToPdfFile(jasperPrint, "./Util/bean.pdf");
            JasperViewer.viewReport(jasperPrint);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object getFieldValue(JRField arg0) throws JRException {
        return null;
    }

    @Override
    public boolean next() throws JRException {
        return false;
    }
}

通过使用iReport 4.6.0,我通过插入表格组件设计了一个报表。

jrxml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ..>
    <subDataset name="New Dataset 1" uuid="50bff6d9-9b98-444b-9851-bdd37ea570e4">
        <queryString><![CDATA[]]></queryString>
        <field name="field1" class="java.lang.String">
            <fieldDescription><![CDATA[field1]]></fieldDescription>
        </field>
        <field name="field2" class="java.lang.String">
            <fieldDescription><![CDATA[field2]]></fieldDescription>
        </field>
    </subDataset>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\\Users\\sachdevh\\Desktop\\reports\\"]]></defaultValueExpression>
    </parameter>
    <parameter name="parameter1" class="java.lang.String">
        <defaultValueExpression><![CDATA[$F{field1}]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="field1" class="java.lang.String">
        <fieldDescription><![CDATA[field1]]></fieldDescription>
    </field>
    <field name="field2" class="java.lang.String">
        <fieldDescription><![CDATA[field2]]></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">
            <componentElement>
                <reportElement uuid="b3e3e08d-91d9-4e01-ae74-abdfc270551a" key="table" x="0" y="0" width="555"
                               height="61"/>
                <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="New Dataset 1" uuid="f5f39e4f-4349-4e5d-9736-03f84a7a7617">
                        <dataSourceExpression>
                            <![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Person.getReportData())]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column uuid="9e2a6076-7f96-40c8-86ae-318b521a4f81" width="90">
                        <jr:tableHeader height="30"/>
                        <jr:tableFooter height="30"/>
                        <jr:columnHeader height="30">
                            <staticText>
                                <reportElement uuid="99411482-c1d5-44fa-aef7-9d9f4bf392ec" x="0" y="0" width="90"
                                               height="30"/>
                                <textElement/>
                                <text><![CDATA[field1]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter height="30"/>
                        <jr:detailCell height="20">
                            <textField>
                                <reportElement uuid="74861d9e-48a8-47ce-a477-81d4f497d483" x="0" y="0" width="90"
                                               height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{field1}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column uuid="84c3b078-0114-452e-b3a3-f1c7dcd1b8cb" width="90">
                        <jr:tableHeader height="30"/>
                        <jr:tableFooter height="30"/>
                        <jr:columnHeader height="30">
                            <staticText>
                                <reportElement uuid="cb87f1a0-bbbf-40b2-bb19-292ecf8d9a18" x="0" y="0" width="90"
                                               height="30"/>
                                <textElement/>
                                <text><![CDATA[field2]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter height="30"/>
                        <jr:detailCell height="20">
                            <textField>
                                <reportElement uuid="84fd788f-24e8-4dfa-8c83-ce388c663f45" x="0" y="0" width="90"
                                               height="20"/>
                                <textElement/>
                                <textFieldExpression><![CDATA[$F{field2}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch"/>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

我认为下面的数据源表达式

<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource(Person.getReportData())]]></dataSourceExpression>
4

1 回答 1

1

您可以分两步解决此问题:

  1. 表格组件移动到标题带(现在它位于列标题带) - 以防止net.sf.jasperreports.engine.JRRuntimeException: Infinite loop creating new page due to column header overflow在数据源有很多元素的情况下发生异常;
  2. 将表的 subDataset dataSourceExpression更改为下面的值,这与您的Java代码相关:
<datasetRun subDataset="New Dataset 1" uuid="f5f39e4f-4349-4e5d-9736-03f84a7a7617">
    <dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
</datasetRun>
于 2012-10-11T07:24:28.220 回答