Not able to use subreports in JasperReports

2019-06-04 03:11发布

My java code for creating Jasper reports is

JasperReport report = JasperCompileManager.compileReport(jrxml);
JasperPrint print = JasperFillManager.fillReport(report,parameters, conn);
JasperExportManager.exportReportToPdfFile(print,filename);

Its running successfully when I m creating reports without using sub reports. When I am inserting any subreport my code fails and exception says

CAUSE: null

MESSAGEnull

LOCAL MESSAGEnull

Please tell me If I need to change my Java code?

I have read this line somewhere to use subreports.

JasperReport subreport = (JasperReport)JRLoader.loadObjectFromLocation("ProductReport.jasper");

Do I need to use this code also? I'm a PHP developer. Don't know much about Java. I used Jasper reports because We are needing creating big PDF. This tool helped us so much. But now I'm stuck with a new report where I need to use subreport thing.

3条回答
神经病院院长
2楼-- · 2019-06-04 03:41

I was using IREPORT 4.1.3 and my jar file in the java code was Jasperreports-3.7.6.jar I read many times that the version of both should be the same. So I tried it and downloaded jasperreports-4.1.3.jar and used it. This worked. Now there is no problem with subreports.

查看更多
放荡不羁爱自由
3楼-- · 2019-06-04 03:47

I believe D. Rodrigues had actually given you the right solution, I have been researching on a similar problem during the past three days with no luck, and finally have it fixed with the suggestion by D. Rodrigues. I realize this is a post a year ago, I'm posting this because I hope it can be helpful to someone encounter similar question in the future.

My situation is: I have a JasperReport that contain multiple layers of subreports, I would like to run it in a Java application built in Netbeans. Initially, I used getResources() for my main report, when I run it, it works fine in the IDE, but when I build it and run from the jar, it gives a "file not found exception", I tried the alternative of using "Inputstream", and use the subreport as a inputsteam, it always gives "error loading input steam", I was frustrated after days of researching, and it worked with this one.

so the idea is you need to get the main report

JasperReport main = (JasperReport)JRLoader.loadObject(this.getClass().getResource("main.jasper"));

and ALL subreports as resources

JasperReport sub1 = (JasperReport)JRLoader.loadObject(this.getClass().getResource("sub1.jasper"));
JasperReport sub2 = (JasperReport)JRLoader.loadObject(this.getClass().getResource("sub2.jasper"));
JasperReport sub3 = (JasperReport)JRLoader.loadObject(this.getClass().getResource("sub3.jasper"));
JasperReport sub4 = (JasperReport)JRLoader.loadObject(this.getClass().getResource("sub4.jasper"));

(there are 4 subreports in the above example)

Since you are passing subreports as "parameters", so you need to have these parameters in your reports, and you need to make sure these parameters reach the layer where they are used, for mine, the layers are Main Sub1 Sub2 Sub3, Sub4

So on my main, I have parameters: sub1, sub2, sub3, sub4, set them all as "Object" in parameter class, set subrepot expression to "$P{sub1}", which will call subreport "Sub1" when run, and in subreport parameters add $P{sub2}, $P{sub3}, $P{sub3}, becasue you are using this parameters in subreports but on Java code, you are only may values to the main report

And so on so forth for the layers after that, my finaly code in Java is:

JasperReport jr = (JasperReport)JRLoader.loadObject(this.getClass().getResource("main.jasper"));
JasperReport sub1 = (JasperReport)JRLoader.loadObject(this.getClass().getResource("sub1.jasper"));
JasperReport sub2 = (JasperReport)JRLoader.loadObject(this.getClass().getResource("sub2.jasper"));
JasperReport sub3 = (JasperReport)JRLoader.loadObject(this.getClass().getResource("sub3.jasper"));
JasperReport sub4 = (JasperReport)JRLoader.loadObject(this.getClass().getResource("sub4.jasper"));
Map para = new HashMap();
para.put("Sub1", sub1);
para.put("Sub2", sub2);
para.put("Sub3", sub3);
para.put("Sub4", sub4);
JasperPrint jp = JasperFillManager.fillReport(jr, para, conn);
JasperViewer.viewReport(jp, false);       

and it works like magic!

If it's still not working, please comment or send me email at: smilelrnr@hotmail.com

I'd love to see what I can do!

查看更多
ゆ 、 Hurt°
4楼-- · 2019-06-04 03:58

This become a lot confunsing for me, but here we go.

First of all you should choose if you will get your report from a .jrxml or a .jasper

If you choose JRXML you have to compile it, this is the code JRXML:

JasperReport report = JasperCompileManager.compileReport(jrxml);

You can notice that you are doing this already, so if you want to load you subreport in the same way you can pass the JRXML file of your subreport in the same way and put this into another variable:

JasperReport subReport = JasperCompileManager.compileReport(subReportjrxml);

When I needed to put a subreport inside my main report I just passed the jasper file as a parameter inside my HashMap, like this:

Map<String, Object> params = new HashMap<String, Object>;

params.put("SUB_REPORT", subReport);

(you put "params" in the fillReport method, but you have to fill only the mainReport, because when you pass your subReport as a parameter it should be filled as well)

Inside the iReport editor, in your main report you have create a parameter with the same name "SUB_REPORT", you can do this in the reportInspector(or something like that) put the type as an Object.

Select your subReportElement and go to the properties, there you can see a property called SubReport Expression, there you put the parameter that you've created.

查看更多
登录 后发表回答