0

我正在为以下报告设计开发iReport 。看看下面给出的格式:-

报告设计

在研究了这个设计之后,我可以总结出以下几点。

  1. 该报告基本上基于员工,他们的工作是根据他们所处理的应用程序以及他们的状态进行总结的。
  2. 对我来说,这似乎是一个交叉表报告。
  3. 如果我使用 POJO 作为数据源,基本上,如果我设计 DTO,它将类似于 POJO,包含员工列表。Employee POJO 本身将是一个具有应用程序类型的集合,而这些应用程序类型又将具有一个状态列表。

我是JasperReports的新手。我期待的是,我们如何设计数据源以实现报表设计。欢迎任何建议。示例 DTO 设计是

包 com.renaissance.model;

import java.util.List;

public class Employee {
  private long employeeNo ;
  private List<ApplicationType> applicationTypes;

  public Employee(long employeeNo, List<ApplicationType> applicationTypes) {
    this.employeeNo = employeeNo;
    this.applicationTypes = applicationTypes;
  }

  public Employee() {

  }

  public long getEmployeeNo() {
    return employeeNo;
  }

  public void setEmployeeNo(long employeeNo) {
    this.employeeNo = employeeNo;
  }

  public List<ApplicationType> getApplicationTypes() {
    return applicationTypes;
  }

  public void setApplicationTypes(List<ApplicationType> applicationTypes) {
    this.applicationTypes = applicationTypes;
  }
}


package com.renaissance.model;

import java.util.List;

public class ApplicationType {
  private long appTypeId ;
  private String appName ;
  private List<ApplicationStatus> applicationStatuses;

  public List<ApplicationStatus> getApplicationStatuses() {
    return applicationStatuses;
  }

  public void setApplicationStatuses(List<ApplicationStatus> applicationStatuses) {
    this.applicationStatuses = applicationStatuses;
  }

  public long getAppTypeId() {
    return appTypeId;
  }

  public void setAppTypeId(long appTypeId) {
    this.appTypeId = appTypeId;
  }

  public String getAppName() {
    return appName;
  }

  public void setAppName(String appName) {
    this.appName = appName;
  }

  public ApplicationType(long appTypeId, String appName) {
    this.appTypeId = appTypeId;
    this.appName = appName;
  }

  public ApplicationType() {

  }
}

package com.renaissance.model;

public class ApplicationStatus {
  private long appStatusId ;
  private String appStatusNameEn ;

  public ApplicationStatus(long appStatusId, String appStatusNameEn) {
    this.appStatusId = appStatusId;
    this.appStatusNameEn = appStatusNameEn;
  }

  public ApplicationStatus() {

  }

  public long getAppStatusId() {
    return appStatusId;
  }

  public void setAppStatusId(long appStatusId) {
    this.appStatusId = appStatusId;
  }

  public String getAppStatusNameEn() {
    return appStatusNameEn;
  }

  public void setAppStatusNameEn(String appStatusNameEn) {
    this.appStatusNameEn = appStatusNameEn;
  }
}

驱动程序

package com.renaissance.reporting;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.renaissance.model.ApplicationStatus;
import com.renaissance.model.ApplicationType;
import com.renaissance.model.Employee;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;

public class GenerateReport {
  public static final String REPORT_DIRECTORY = "c:\\reporting";
  public static void main(String[] args) throws IOException, JRException {
    JRDataSource jrDataSource = prepareDataSource();
    JasperPrint jasperPrint =  generateJasperReportPrint(jrDataSource,REPORT_DIRECTORY+"\\crosstabTest.jasper");
    JRPdfExporter pdfExporter = new JRPdfExporter();
    pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
    pdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,
        REPORT_DIRECTORY + "//" + "test" + ".pdf");
    System.out.println("Exporting report...");
    pdfExporter.exportReport();
    System.out.println("Done!");

  }

  private static JRDataSource prepareDataSource(){
    List<ApplicationStatus> applicationStatuses1 = new ArrayList<ApplicationStatus>();
    List<ApplicationStatus> applicationStatuses2 = new ArrayList<ApplicationStatus>();
    List<ApplicationStatus> applicationStatuses3 = new ArrayList<ApplicationStatus>();
    List<ApplicationType> applicationTypes1 = new ArrayList<ApplicationType>();
    List<ApplicationType> applicationTypes2 = new ArrayList<ApplicationType>();
    List<ApplicationType> applicationTypes3 = new ArrayList<ApplicationType>();



    applicationStatuses1.add(new ApplicationStatus(2,"Approved")) ;
    applicationStatuses1.add(new ApplicationStatus(3,"Rejected")) ;
    applicationStatuses1.add(new ApplicationStatus(4,"NSD Cleared"));

    applicationStatuses2.add(new ApplicationStatus(10,"Approved")) ;
    applicationStatuses2.add(new ApplicationStatus(5,"Rejected")) ;
    applicationStatuses2.add(new ApplicationStatus(6,"NSD Cleared"));



    ApplicationType applicationType1 = new ApplicationType();
    ApplicationType applicationType2 = new ApplicationType();
    ApplicationType applicationType3 = new ApplicationType();
    ApplicationType applicationType4 = new ApplicationType();
    applicationType1.setAppTypeId(1);
    applicationType1.setAppName("Short Term");
    applicationType1.setApplicationStatuses(applicationStatuses1);

    applicationType2.setAppTypeId(2);
    applicationType2.setAppName("Long Term");
    applicationType2.setApplicationStatuses(applicationStatuses2);

    applicationType3.setAppTypeId(3);
    applicationType3.setAppName("Outpass");
    applicationType3.setApplicationStatuses(applicationStatuses1);


    Employee employee1 = new Employee();
    Employee employee2 = new Employee();
    employee1.setEmployeeNo(1);
    employee2.setEmployeeNo(2);
    applicationTypes1.add(applicationType1);
    applicationTypes1.add(applicationType2);
    applicationTypes2.add(applicationType3);
    employee1.setApplicationTypes(applicationTypes1);
    employee2.setApplicationTypes(applicationTypes2);

    List<Employee> employees = new ArrayList<Employee>();
    employees.add(employee1);
    employees.add(employee2);

    return new JRBeanCollectionDataSource(employees);
  }

  private static JasperPrint generateJasperReportPrint(JRDataSource dataSource, String reportPath)
      throws JRException, IOException {
    JasperPrint jasperPrint = null;
    FileInputStream inputStream = null;
    try {

      inputStream = new FileInputStream(new File(reportPath));
      jasperPrint = JasperFillManager.fillReport(inputStream, new HashMap(), dataSource);
    }
    catch(IOException e) {

      if (inputStream != null) {
        try {
          inputStream.close();
        }
        catch(IOException e1) {
          e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
      }
    }
    return jasperPrint;
  }
}
4

1 回答 1

0

我会使用 iReport 的 Crosstab 元素来汇总此处显示的数据,它应该适用于您的用例。

至于你的数据结构,我会尝试去规范化它。

因此,如果您有嵌套对象,只需将其视为单个对象,并让 iReport 根据交叉表的分组标准聚合数据。

如果您甚至没有在 POJO 中工作并且它正在从 SQL 数据库中提取数据,那将是相似的。您将有一袋记录,其中包含 Emp No、Emp Name、Application Type、Status。

从那里交叉表设计相对容易,通过向导(如果您使用 iReport)将 Emp No 指定为您的行组,将 Status 指定为您的列组,指定状态计数作为您的 Aggregate Measure,并添加 Emp Name,和应用程序类型作为度量,没有任何东西作为您的计算。将布局修改为您的规范,它应该可以正常工作。

于 2013-09-09T07:20:09.160 回答