Jasper报表:子报表在主报表中显示空值,数据源是一个Java列表 [英] Jasper Reports: Subeport showing null values in Main Report, datasource is a java List

查看:51
本文介绍了Jasper报表:子报表在主报表中显示空值,数据源是一个Java列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所拥有的

我的主报告显示子报告中的所有值均为空.

我想要的

我正在通过一个名为subData的参数将数据从MainReport传递到subReport,如下面的MainReport JRXML片段所示.

NB :其列表

我在MainReport中的subReport片段如下所示,dataSource表达式显然是"$ P {subData}"

我的主要Java类通过如下所示的参数(名为subDataOrion)传递List(名为subData)的实例.

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 = new ArrayList();
    //subData.add(subDataWrap.getSubData());
    subData.add(new SubreportObject("Kevin",20));
    subData.add(new SubreportObject("Jane",20));
    subData.add(new SubreportObject("Mike",20));
    subData.add(new SubreportObject("Simon",20)); 
    //subData.add(new SubreportObject("Naomi",25));
    //subData.add(new SubreportObject("Pat",20));


    //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
    //JRDataSource subx = new JRBeanCollectionDataSource(subData);
    para.put("subDataOrion",subData);

    //JasperPrint jp = JasperFillManager.fillReport(sub, para,new JRBeanCollectionDataSource(subData));      
    //JasperPrint jp = JasperFillManager.fillReport(main, para,new JRTableModelDataSource(createMasterData()));


     JasperCompileManager.compileReportToFile(
            "src/subrepoexample/subReport.jrxml", 
            "src/subrepoexample/subReport.jasper"); 


    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"},       
                       {"Data","Data","Data"},   
                       {"Data","Data","Data"},   
                       {"Data","Data","Data"},  
                      };
    DefaultTableModel dtm = new DefaultTableModel(data,cols);

return dtm;}

}

用于创建subReport对象的SubReport类

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

  }

我知道这里有两个问题,因为子报表数据是混合的,所以数据的安排问题.或如第一个屏幕截图所示,使用主要报告数据进行加扰.我认为可以按组解决此问题,这不是我的问题.

我的问题:

我首先需要消除'null'值,这样我才能进入使用组表达式的下一个阶段.卡在这里,不知道出什么问题了吗?至少我问了这个问题,至少出现了空"值,之前它们完全没有出现.

为什么列表中的数据显示为空",如果分别进行编译,它们的编译效果会很好.

很抱歉在此问题的较早版本上草率.谢谢大家.

我添加了如下所示的JRBeanCollection

甚至如下将参数类更改为JRDataSource

但仍显示空值..???

解决方案

您缺少JRBeanCollectionDatasource,List本身不是数据源,除非它实现了JRDataSource接口应该由JRDataSource包装/适配. /p>

WHAT I HAVE

My Main Report shows all values are null from my subreport.

WHAT I WANT

I am passing data to subReport from MainReport through a parameter called subData as shown in the MainReport JRXML snippet below.

NB: Its a List

My subReport snippet in the MainReport is given below, the dataSource expression is clearly "$P{subData}"

My main java class passes an instance of a List (named subData) through a parameter (named subDataOrion) shown below.

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 = new ArrayList();
    //subData.add(subDataWrap.getSubData());
    subData.add(new SubreportObject("Kevin",20));
    subData.add(new SubreportObject("Jane",20));
    subData.add(new SubreportObject("Mike",20));
    subData.add(new SubreportObject("Simon",20)); 
    //subData.add(new SubreportObject("Naomi",25));
    //subData.add(new SubreportObject("Pat",20));


    //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
    //JRDataSource subx = new JRBeanCollectionDataSource(subData);
    para.put("subDataOrion",subData);

    //JasperPrint jp = JasperFillManager.fillReport(sub, para,new JRBeanCollectionDataSource(subData));      
    //JasperPrint jp = JasperFillManager.fillReport(main, para,new JRTableModelDataSource(createMasterData()));


     JasperCompileManager.compileReportToFile(
            "src/subrepoexample/subReport.jrxml", 
            "src/subrepoexample/subReport.jasper"); 


    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"},       
                       {"Data","Data","Data"},   
                       {"Data","Data","Data"},   
                       {"Data","Data","Data"},  
                      };
    DefaultTableModel dtm = new DefaultTableModel(data,cols);

return dtm;}

}

SubReport class that creates the subReport objects

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

  }

I am aware there are two issues here, the arrangement issue of the data since the subreport data is mixed. or scrambled with the main report data as shown in the first screen shot. This I think can be fixed by groups, which is not my question here.

MY QUESTION:

I need first to eliminate the 'null' values so that I can go to the next stage of using group expression. Am stuck here and not sure what is wrong? At least I asked this question and at least 'null' values appeared, earlier they were completely not appearing.

Why is the data in the List appearing as 'null', when compiled separately, they compile wonderfully.

EDIT 1:

Sorry for being sloppy on the earlier version of this question. Thank you people.

EDIT 2:

I have added the JRBeanCollection as shown below

Even changed the parameter class to JRDataSource as below

but still shows null values..???

解决方案

You are missing the JRBeanCollectionDatasource, a List is not a datasource per se, unless it implements the JRDataSource interface should be wrapped/adapted by a JRDataSource.

这篇关于Jasper报表:子报表在主报表中显示空值,数据源是一个Java列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆