简体   繁体   English

无法在JasperReports中使用子报表

[英]Not able to use subreports in JasperReports

My java code for creating Jasper reports is 我创建Jasper报告的java代码是

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 原因:null

MESSAGEnull MESSAGEnull

LOCAL MESSAGEnull LOCAL MESSAGEnull

Please tell me If I need to change my Java code? 请告诉我如果需要更改我的Java代码?

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. 我是一名PHP开发人员。 Don't know much about Java. 不太了解Java。 I used Jasper reports because We are needing creating big PDF. 我使用了Jasper报告,因为我们需要创建大型PDF。 This tool helped us so much. 这个工具给了我们很多帮助。 But now I'm stuck with a new report where I need to use subreport thing. 但是现在我遇到了一个新的报告,我需要使用子报表。

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. 我使用的是IREPORT 4.1.3,java代码中的jar文件是Jasperreports-3.7.6.jar我多次读过两个版本应该是相同的。 So I tried it and downloaded jasperreports-4.1.3.jar and used it. 所以我尝试了并下载了jasperreports-4.1.3.jar并使用它。 This worked. 这很有效。 Now there is no problem with subreports. 现在子报表没有问题。

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 首先,您应该选择是否从.jrxml.jasper获取报告

If you choose JRXML you have to compile it, this is the code JRXML: 如果你选择JRXML你必须编译它,这是代码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: 您可以注意到您已经这样做了,所以如果您想以相同的方式加载子报表,您可以以相同的方式传递子报表的JRXML文件并将其放入另一个变量中:

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: 当我需要在主报表中放置一个子报表时,我只是将jasper文件作为参数传递给我的HashMap,如下所示:

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) (你在fillReport方法中加入了“params”,但是你必须只填充mainReport,因为当你将subReport作为参数传递时,它也应该被填充)

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. 在iReport编辑器中,在主报表中,您创建了一个名为“SUB_REPORT”的参数,您可以在reportInspector(或类似的东西)中将此类型作为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. 选择你的subReportElement并转到属性,在那里你可以看到一个名为SubReport Expression的属性,你可以在那里放置你创建的参数。

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. 我相信D.罗德里格斯实际上已经给了你正确的解决方案,我在过去的三天里一直在研究类似的问题而没有运气,最后根据D.罗德里格斯的建议解决了这个问题。 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. 我的情况是:我有一个包含多层子报表的JasperReport,我想在Netbeans内置的Java应用程序中运行它。 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. 最初,我使用getResources()作为我的主报告,当我运行它时,它在IDE中工作正常,但是当我构建它并从jar运行时,它给出了“文件未找到异常”,我尝试了替代方案使用“输入流”,并使用子报告作为输入组,它总是给出“错误加载输入蒸汽”,经过几天的研究后我感到很沮丧,并且它与此一起工作。

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) (上例中有4个子报告)

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 由于您将子报表作为“参数”传递,因此您需要在报表中包含这些参数,并且需要确保这些参数到达使用它们的图层,对于我的图层,这些图层是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 所以在我的main上,我有参数:sub1,sub2,sub3,sub4,在参数类中将它们全部设置为“Object”,将subrepot表达式设置为“$ P {sub1}”,这将在运行时调用子报告“Sub1”,并在子报表参数中添加$ P {sub2},$ P {sub3},$ P {sub3},因为您在子报表中使用此参数但是在Java代码中,您只能在主报表中使用值

And so on so forth for the layers after that, my finaly code in Java is: 等等之后,我在Java中的最终代码是:

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 如果仍然无法正常工作,请发送电子邮件至:smilelrnr@hotmail.com

I'd love to see what I can do! 我很想看看我能做些什么!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM