我正在使用 iReport 进行以下报告设计。请看下面给出的格式: -
在研究这个设计之后,我可以得出以下几点。
我是 JasperReports 的新手。我期待的是,我们如何设计数据源 为了实现报告设计。欢迎任何建议。样本DTO设计是
package 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;
}
}
答案 0 :(得分:0)
我会使用iReport的Crosstab元素来聚合这里提供的数据,它应该适用于您的用例。
至于你的数据结构,我会尝试对其进行去规范化。
因此,如果您有嵌套对象,只需将其视为单个对象,并让iReport根据交叉表的分组条件聚合数据。
如果你甚至没有从POJO工作并且它从SQL数据库中提取数据,那将会是类似的。您将获得一袋包含Emp No,Emp Name,Application Type,Status的记录。
从那里交叉表设计相对容易,通过向导(如果您使用的是iReport)将Emp No指定为您的行组,将状态指定为您的列组,指定状态计数作为您的聚合度量,并添加Emp Name和Application Type as Measures,无需计算。将布局修改为您的规范,它应该可以很好地工作。