How to get subreport name from JasperReport object (jasper compressed file) via API?

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

I can get fields and datasets through

report.getMainDataset().getFields();

However, I can’t get nested reports, I tried to get them through

report.getAllBands();

and then using the for clause

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

That way I can get multiple classes JRBaseSubreport, and that is as far as I can get. I can access the subtitle elements, but I cannot get the subtitle names.

Is there any way to do this?

+3
source share
1 answer

, JasperReport. .

, , JRBaseSubreport, , , .

, , , , - .

, ,

, (subreport )

//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 , .. , , ( )

+3

Source: https://habr.com/ru/post/1651547/


All Articles