无法在JasperReports中使用子报表 [英] Not able to use subreports in JasperReports

查看:139
本文介绍了无法在JasperReports中使用子报表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建Jasper报告的java代码是

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


原因:null

CAUSE: null

MESSAGEnull

MESSAGEnull

LOCAL MESSAGEnull

LOCAL MESSAGEnull

请告诉我如果需要更改我的Java代码?

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");

我是否还需要使用此代码?我是一名PHP开发人员。不太了解Java。我使用了Jasper报告,因为我们需要创建大型PDF。这个工具给了我们很多帮助。但是现在我遇到了一个新报告,我需要使用子报告。

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.

推荐答案

我相信D. Rodrigues实际上给你正确的解决方案,我在过去的三天里一直在研究类似的问题而没有运气,最后根据D.罗德里格斯的建议解决了这个问题。我发现这是一年前发布的帖子,我发布这个帖子是因为我希望将来有人会遇到类似的问题。

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.

我的情况是:我有一个包含多层子报表的JasperReport,我想在Netbeans中构建的Java应用程序中运行它。最初,我使用getResources()作为我的主报告,当我运行它时,它在IDE中工作正常,但是当我构建它并从jar运行时,它给出了文件未找到异常,我尝试了替代方案使用输入流,并使用子报告作为输入组,它总是给出错误加载输入蒸汽,经过几天的研究后我感到很沮丧,并且它适用于这个。

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.

所以你需要获得主报告

JasperReport main = (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"));

(上例中有4个子报告)

(there are 4 subreports in the above example)

由于您将子报表作为参数传递,因此您需要在报表中包含这些参数,并且需要确保这些参数到达使用它们的图层,对于我的图层,这些图层是
Main
Sub1
Sub2
Sub3,Sub4

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

所以在我的主要部分,我有参数:sub1,sub2,sub3,sub4 ,将它们全部设置为参数类中的对象,将子版本表达式设置为$ P {sub1},这将在运行时调用子报表Sub1,并在子报表参数中添加$ P {sub2},$ P {sub3} ,$ P {sub3},因为你在子报告中使用这些参数但是在Java代码中,你只能在主报告中使用值

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

依此类推在那之后,我在Java中的最终代码是:

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

它就像魔法一样!

如果它仍然无法正常工作,请发送电子邮件至或发送电子邮件至:smilelrnr@hotmail.com

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!

这篇关于无法在JasperReports中使用子报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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