gpt4 book ai didi

java - JASPER REPORT : Subreport nightmare, 数据源是列表

转载 作者:行者123 更新时间:2023-12-02 01:12:57 24 4
gpt4 key购买 nike

无法查看带有子报表的主报表...

主要 Java 类

   public class SubRepoExample {

public static void main(String[] args) throws JRException {
SubRepoExample repo = new SubRepoExample();
repo.combineMasterAndSubreport();
}

public void combineMasterAndSubreport() throws JRException{

JasperReport main = JasperCompileManager.compileReport("src/subrepoexample/masterReport.jrxml");

JasperReport sub = JasperCompileManager.compileReport("src/subrepoexample/subReport.jrxml");

//create a list for holding the subreport object



SubreportWrapper subDataWrap = new SubreportWrapper();

List<SubreportObject> subData = subDataWrap.getSubData();

Map para = new HashMap();
//pass the report itself through a parameter
para.put("SUBREPORT", sub);
//pass the list to parameter
para.put("subData",subData);


JasperPrint jp = JasperFillManager.fillReport(main, para,new JRTableModelDataSource(createMasterData()));
JasperViewer.viewReport(jp, false);

}

public DefaultTableModel createMasterData(){

String [] cols = {"COL_1","COL_2","COL_3"};
Object[][] data = {{"Data","Data","Data"},
{"Data","Data","Data"},
};
DefaultTableModel dtm = new DefaultTableModel(data,cols);

return dtm;}

}

SubreportObject 类负责创建最终放置在列表中的子报表数据。

 public class SubreportObject {
String name;
int age;

public SubreportObject(String name,int age){
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

}

Bean工厂类

 public class SubreportWrapper {
//String name;
List<SubreportObject> subData;

// public SubreportWrapper(List<SubreportObject> subData){
// this.subData = subData;
// }

public List<SubreportObject> getSubData() {

subData = new ArrayList();
subData.add(new SubreportObject("Jane",20));
subData.add(new SubreportObject("Mike",25));
subData.add(new SubreportObject("Kev",20));
return subData;
}
public void setSubData(List<SubreportObject> subData) {
this.subData = subData;
}
}

主 jrxml 和子报告 jrxml 中的 JRXML 片段 field declaration of a list both in main report and subreport

通过 JRBeanCollection 的数据源片段 datasorceSUBREPORT_DIR 已就位

错误堆栈跟踪 error

我做错了什么?

最佳答案

您正在设置 JRBeanCollectionDatasource 构造函数,其中子数据作为字段 $F{subData},请改用参数:$P{subData}

无需在 SUBREPORT 参数中传递子报表,因为 .jasper 文件将根据 subreportExpressionSUBREPORT_DIR 参数加载。

关于java - JASPER REPORT : Subreport nightmare, 数据源是列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59103670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com