贾斯珀报告:子报告的噩梦,数据源是列表

时间:2019-11-29 11:09:37

标签: java jasper-reports subreport

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

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;
    }  
  }

Main jrxml和Subreport jrxml中的JRXML代码段 field declaration of a list both in main report and subreport

通过JRBeanCollection获得的数据片段 datasorce SUBREPORT_DIR到位

错误堆栈跟踪 error

我在做什么错了?

1 个答案:

答案 0 :(得分:2)

您要设置JRBeanCollectionDatasource构造函数,并将子数据作为字段$F{subData},使用参数:$P{subData}

由于将根据SUBREPORTsubreportExpression参数加载.jasper文件,因此无需在SUBREPORT_DIR参数中传递子报表。