使用Bean Collection DataSource的Jasper报告生成一个空页面

时间:2018-04-20 11:11:09

标签: java jasper-reports

我正在尝试使用Jasper Reports 6.5.1使用JRBeanCollectionDataSource将数据从Java传递到报表中的字段属性。

我已经创建了JavaBean和报告,并且所有数据似乎都没有任何问题,但最终的结果是一个我无法解决的空白页面。任何帮助将不胜感激。请参阅下面的代码......

提前致谢

代码生成报告

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class Jasper{

    public static void main(String[] args) throws JRException, FileNotFoundException {

        ArrayList<JasperBean> list = new ArrayList<JasperBean>();

        String reportFile = "C:\\test\\tester.jrxml";
        String output = "C:\\test\\Jasper.pdf";

        JasperBean data = new JasperBean();

        data.setName("Freddie");
        data.setSurname("Wilson");
        data.setTitle("Mr.");

        list.add(data);
        JasperReport jr = JasperCompileManager.compileReport(reportFile);
        JRDataSource dataSource = new JRBeanCollectionDataSource(list);
        Map<String, Object> params = new HashMap<>();
        params.put("DataSource", dataSource);
        JasperPrint print = JasperFillManager.fillReport(jr, params);
        FileOutputStream out = new FileOutputStream(new File(output));
        JasperExportManager.exportReportToPdfStream(print, out);        
    }
}

的JavaBean

import java.io.Serializable;

public class JasperBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String name;
    private String Surname;
    private String title;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return Surname;
    }

    public void setSurname(String surname) {
        Surname = surname;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }   
}

此处为报告源XML

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.5.1.final using JasperReports Library version 6.5.1  -->
<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="tester" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2dabb962-4ca0-4006-881b-9c608349031a">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <field name="DataSource" class="testing.JasperBean"/>
    <field name="title" class="java.lang.String"/>
    <field name="name" class="java.lang.String"/>
    <field name="surname" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <detail>
        <band height="311" splitType="Stretch">
            <textField>
                <reportElement x="50" y="162" width="232" height="18" uuid="00051b07-d9a7-4531-a757-70afa89f17cf"/>
                <textFieldExpression><![CDATA[$F{title} + " " + $F{name} + " " + $F{surname}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

1 个答案:

答案 0 :(得分:1)

在main()方法中使用:

JasperPrint print = JasperFillManager.fillReport(jr, params, dataSource);

从jrxml文件中删除:

<field name="DataSource" class="testing.JasperBean"/>