如何将List <object []>作为数据源发送给jasper报告?</object []>

时间:2015-03-12 11:12:05

标签: java hibernate jasper-reports ireport

我想从两个表生成一个JasperReports报告。表格为empdesig

尊重的bean类是

@Entity
@Table(name = "desg")
public class Designation {

    @Id
    @Column(name="Empid")
    private int eId;
    @Column(name="Designation")
    private String Designation;

    // getters and setters
}

@Entity
@Table(name = "emp")
public class Employee {

    @Id
    @GeneratedValue
    @Column(name="Eid")
    private int eId;
    @Column(name="Name")
    private String name;
    @Column(name="Address")
    private String address;
    @Column(name="Salary")
    private int salary;

    // getters and setters
}

只有一个表的报告工作正常。我使用Hibernate创建bean对象列表。我要求报告如下,

    List<Object[]> lst= sessio.createQuery("select e.eId, e.name, d.Designation From Employee e, Designation d where e.eId=d.eId ").list();

    String reportSourceFile="E:\\classes\\report4.jrxml";

    JasperReport jasperReport = null;
    JasperDesign jasperDesign = null;
    Map parameters = new HashMap();  
    parameters.put("Title", "The EMP Report"); 
    try {
    jasperDesign = JRXmlLoader.load(reportSourceFile);
    jasperReport = JasperCompileManager.compileReport(jasperDesign);
    byte[] byteStream = JasperRunManager.runReportToPdf(jasperReport, parameters, new JRBeanCollectionDataSource(emplst)); // what to change instead of emplst
    OutputStream outStream = response.getOutputStream();
    response.setHeader("Content-Disposition","filename=myReport.pdf");
    response.setContentType("application/pdf");
    response.setContentLength(byteStream.length);
    outStream.write(byteStream,0,byteStream.length); 

    } catch (JRException e1) {
        e1.printStackTrace();
    }

如果我发送emplst它可以正常工作,但我想在报告中打印lst。如何将lst对象发送到 JasperReports 的报告以及如何进入iReport工具?

2 个答案:

答案 0 :(得分:0)

在JRXML中使用导入标记

      <import value="yourPackageName.Employee">

并在数据类型为java.lang.Object的JRXML中创建参数,可以说myEmployee是参数名称

然后将Employee Object作为参数传递给JRXML

Map<String,Object> map=new HashMap<String,Object>();
map.put("myEmployee",yourEmployeeObject);

现在您的员工类对象出现在JRXML中,您可以从JRXML调用getter来获取对象状态(i.e attributes of Object

答案 1 :(得分:0)

你可以:
1)为您的联接对象创建包装类以传递给JRBeanCollectionDataSource

2)创建自己的JRDataSource实现

3)根据sql填充报告,而不是数据源