假設我有2種豆:顯示爲不同的豆子收集報告
- 獎
- 員工
我在集合中添加他們兩人。基於這個集合,我想生成一個報告使用DynamicJasper使用JRBeanCollectionDataSource。
我能夠爲單個bean生成報告,但是爲了收集不同的bean我無法創建報告 - 我收到一個錯誤消息。
是否有可能一次爲兩個不同的bean創建報告?
有沒有其他解決方案可以解決這個問題?
假設我有2種豆:顯示爲不同的豆子收集報告
我在集合中添加他們兩人。基於這個集合,我想生成一個報告使用DynamicJasper使用JRBeanCollectionDataSource。
我能夠爲單個bean生成報告,但是爲了收集不同的bean我無法創建報告 - 我收到一個錯誤消息。
是否有可能一次爲兩個不同的bean創建報告?
有沒有其他解決方案可以解決這個問題?
對此的替代,可以使用子報表並通過List<Award>
到MainReport和List<Employee>
到子報表,這樣做,你可以設置你子報表爲您MainReport的延伸,例如:
MainReport: |FieldAward1|FieldAward2|FieldAward3|...|YOUR SUBREPORT HERE|
子報表: |FieldEmployee1|FieldEmployee3|FieldEmployee3|...
編輯: 即使它看起來基本,我要告訴你,你應該在某種程度上使每個對象的排序列表內的一個列表與另一內其repective對象相匹配。
隨着所提供的信息,這意味着有Award
和Employee
之間沒有任何關係。 如果是這種情況,您可以創建一個自定義數據源來處理這個問題。爲了完整起見,我將包括掐滅獎和員工類自定義數據源稱爲MixedDataSource一起。 (這裏將在這裏列出了很多代碼,但我堅持一秒鐘)。
Award.java
package test;
public class Award {
private String shortName;
private String description;
public Award(String shortName, String description) {
super();
this.shortName = shortName;
this.description = description;
}
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Employee.java
package test;
public class Employee {
private String name;
private String position;
public Employee(String name, String position) {
super();
this.name = name;
this.position = position;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
MixedDataSource.java
package test;
import java.util.List;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
@SuppressWarnings("rawtypes")//have to add this because we are not using generics
public class MixedDataSource implements JRDataSource {
private List rows;
private int index=0;
public MixedDataSource(List rows) {
super();
this.rows = rows;
}
@Override
public Object getFieldValue(JRField arg0) throws JRException {
Object obj = rows.get(index);
if (obj instanceof Award){
Award row = (Award)obj;
//now get the field name
if (arg0.getName().equals("shortName")){
return row.getShortName();
} else if(arg0.getName().equals("description")){
return row.getDescription();
}
} else if (obj instanceof Employee){
Employee row = (Employee)obj;
if (arg0.getName().equals("name")){
return row.getName();
} else if(arg0.getName().equals("position")){
return row.getPosition();
}
}
//means we don't know what to do with it, so just return null
return null;
}
@Override
public boolean next() throws JRException {
//This method is used by jasper to tell us they are moving to the next row.
//So increment the index and return true if there are still more rows, if not
//return false
index = index+1;
if(index < rows.size()){
return true;
}
return false;
}
}
這W¯¯ ork,但可能不是你想要的方式。我的假設是,有一個獎和員工之間的關係,你想要做某種類型的分組和排序。基本上你想要在同一行上有一個Award
和Employee
。您可能希望按獎勵分組並列出員工。你可能想要做相反的事情,並按員工和名單獎勵分組。 如果是這樣,忽略以上所有內容,這是沒用的。
你真正需要做的是用你所擁有的信息創建一個新的bean(我們可以稱它爲EmployeeAward
)。如果你正在使用sql查詢來做到這一點,這可能非常容易,或者如果你使用的是類似hibernate的東西,可能需要更多的工作。你基本上只是說給所有的獎項,它給你一個清單。如果你正在做後者,它可能會更容易下臺到SQL或HSQL(我認爲他們稱之爲)並手動編寫查詢。
因此創建一個名爲EmployeeAward
的類。這裏是我的: 包測試;
public class EmployeeAward {
private String employeeName;
private String employeePosition;
private String shortName;
private String description;
public EmployeeAward(String employeeName, String employeePosition,
String shortName, String description) {
super();
this.employeeName = employeeName;
this.employeePosition = employeePosition;
this.shortName = shortName;
this.description = description;
}
public EmployeeAward(Employee employee, Award award) {
super();
this.employeeName = employee.getName();
this.employeePosition = employee.getPosition();
this.shortName = award.getShortName();
this.description = award.getDescription();
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeePosition() {
return employeePosition;
}
public void setEmployeePosition(String employeePosition) {
this.employeePosition = employeePosition;
}
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
現在,通過任何手段必須創建這些對象的列表,所以你必須在那裏實例化的List<EmployeeAward> emplyeeAwards
。此列表中的每個項目將成爲報告中的一行。現在這裏是很好的部分,創建你的數據源:
JRDataSource datasource = new JRBeanCollectionDataSource(employeeAwards);
然後通過這樣的正常和你的工作完成。
最後,我想說你可以用JasperReports做很多很酷的事情,但是我看到很多人在做的事情使它變得比它需要的更復雜。你會發現JasperReports就像我所說的平面數據源(即列表中的相同對象,列表中的每個項目是一行等)。如果你這樣做,生活變得更容易,更愉快。
jschoen謝謝你的回覆哥們,真的很值得你嘗試解決我的問題你猜對了員工與獎勵有着多對多的關係,所以員工有一個獎項的集合。 –
因此,當顯示員工時,我期待那個動態的碧玉也會顯示獎勵的集合,但我想它不會。我試圖解決它使用SQL查詢希望它的作品 –
您應該發佈您的代碼和異常的堆棧跟蹤 –
您可以查看[JavaBean Data Sources](http://jasperforge.org/uploads/publish/jasperreportswebsite/trunk/sample.reference/datasource/index.html#javabeandatasources )樣本 –
我不確定,但我認爲這是不可能的。 –