如何通过API从JasperReport对象(.jasper编译文件)获取子报表名称? [英] How to get subreport name from JasperReport object (.jasper compiled file) via API?

查看:143
本文介绍了如何通过API从JasperReport对象(.jasper编译文件)获取子报表名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个包含子报告的主报告文件。它被编译为.jasper,我通过将其加载到 JaserReport 类来访问它。

There's a main report file containing subreports. It is compiled to .jasper and I access it by loading it into JaserReport class.

我能够获得字段和数据集

I am able to get fields and datasets trough

report.getMainDataset().getFields();

然而我无法获得子报告,我一直试图让他们通过

However I am unable to get subreports, I've been trying to get them trough the

report.getAllBands();

然后使用for子句

 for (int i = 0; i < bands.length; i++) {
            JRElement[] element = bands[i].getElements(); 
  }

这样我可以得到一些 JRBaseSubreport 类,它是我能得到的。我可以访问子报表的元素,但无法获取子报表的名称。

This way I can get some JRBaseSubreport classes, and it is as far as I can get. I can access the elements of subreports, but can't get the names of subreports.

有没有办法做到这一点?

Is there any way to do that?

推荐答案

无法通过将主报表加载到 JasperReport 对象中来获取子报表中的数据。此对象仅包含主报表及其元素。

You can not get data in subreport by only loading the main report into JasperReport object. This object only contains the main report and it's elements.

相对于子报表的元素是 JRBaseSubreport ,它引用了与主报表中的子报表标签相关的数据,因此它不包含实际的报表对象。

The element relative to the subreport is a JRBaseSubreport, which has reference to data relative to subreport tag in main report, hence it does not contain the actual report object.

子报表将由填充加载,具体取决于表达式,您实际上可以根据数据源的值加载不同的子报表所以jasper报告无法知道在报告填写之前要加载哪个子报告。

The subreport will be loaded by the filler depending on the expressions, you could actually load a different sub report depending on the values of your datasource so jasper report can't know what sub report to load until the report is filled.

但是你可以访问它将使用的表达式加载子报表,这可能足够你的情况

But you can access the expression it will use to load the subreport and this maybe can be enough in your case

关于如何访问表达式的示例(第一个细节带中的子报表)

//I know the subreport is in first detail band so I access this directly 
JRBaseBand detailBand1 = (JRBaseBand) report.getDetailSection().getBands()[0];
List<JRChild> elements = detailBand1.getChildren(); //Get all children
for (JRChild child : elements) {
    if (child instanceof JRBaseSubreport){ //This is a subreport
        JRBaseSubreport subreport = (JRBaseSubreport)child;
        String expression= ""; //Lets find out the expression used
        JRExpressionChunk[] chunks = subreport.getExpression().getChunks();
        for (JRExpressionChunk c : chunks) {
            expression +=c.getText();
        }
        System.out.println(expression); 
        //Here you could do code to load the subreport into a JasperReport object
    }
}

使用此数据,您可以手动将子报表加载到另一个 JasperReport 对象和访问名称,字段等。当然,如果您有一个复杂的表达式,您的代码将需要反映这一点(反向参数或来自数据源的数据)

With this data you can load the subreport manually into another JasperReport object and access name, fields etc. Naturally if you have a complex expression your code will need to reflect this (retrive parameters or data from datasource)

这篇关于如何通过API从JasperReport对象(.jasper编译文件)获取子报表名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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