gpt4 book ai didi

java - Jasper Reports 中的子报表

转载 作者:行者123 更新时间:2023-11-30 11:58:38 26 4
gpt4 key购买 nike

我有两个表格要打印在同一页上。这些表的数据将从自定义数据源的 map 中提供。我需要实现它。我用谷歌搜索实现这个任务。

那时我接触到子报表的概念,但是我并没有得到太多关于如何实现子报表的信息。

我知道如何创建子报告。但是我不知道如何用自定义数据源填充子报告。那是我的问题。请帮助我实现这一目标。

注意:我尝试实现子报表,但出现错误。这是我用于编译子报表的代码:

JasperPrint print = new JasperPrint();
JRPdfExporter exporter = new JRPdfExporter();
JasperDesign design, design1;
JasperReport report, report1;
JasperReport jasperReport;
JasperDesign jd1;

jd1 = JRXmlLoader.load("D:\\sub_report1.jrxml");
jasperReport = JasperCompileManager.compileReport(jd1);
Map parameters = new HashMap();
parameters.put("ReportTitle", "Table Report");
parameters.put("DataFile", "TableSource.java");
parameters.put("Subreport_1", jasperReport);

design = JRXmlLoader.load("D:\\sub_report2.jrxml");
report = JasperCompileManager.compileReport(design);
parameters.put("Subreport_2", report);

design1 = JRXmlLoader.load("D:\\master_report.jrxml");
report1 = JasperCompileManager.compileReport(design1);
JasperFillManager.fillReportToFile(report1, "D:\\master_report.jrprint", parameters, new TableDataSource());

exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "D:K\\Report.pdf");
exporter.exportReport();

JasperViewer.viewReport(print);

这里我分别编译了subreport1 & subreport2,并将它们作为参数添加到map中。然后我在通过 masterreports 填充它时遇到错误。请帮我编写报告。

编译器错误的堆栈跟踪:

Exception in thread "main" net.sf.jasperreports.engine.JRRuntimeException: Unkown print order 0. at net.sf.jasperreports.engine.fill.JRFillSubreport.initSubreportFiller(Unknown Source) at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(Unknown Source) at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(Unknown Source) at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(Unknown Source) at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(Unknown Source) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(Unknown Source) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(Unknown Source) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(Unknown Source) at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(Unknown Source) at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(Unknown Source) at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(Unknown Source) at net.sf.jasperreports.engine.fill.JRFiller.fillReport(Unknown Source) at net.sf.jasperreports.engine.JasperFillManager.fillReport(Unknown Source) at net.sf.jasperreports.engine.JasperFillManager.fillReportToFile(Unknown Source) at ReportTableCompiler.main(ReportTableCompiler.java:53)

最佳答案

如果您只有一个详细信息带并且您的子报表表达式类似于“SUBREPORT_1.jasper”,您可以使用它来编译多个子报表

   JRDesignBand jrBand = (JRDesignBand) jasperDesign.getDetailSection().getBands()[0];
JRElement[] jrElements = jrBand.getElements();
for (JRElement jrElement : jrElements) {
if (jrElement instanceof JRDesignSubreport) {
JRDesignSubreport subReportDesign = (JRDesignSubreport) jrElement;
JRExpression jrExpression = subReportDesign.getExpression();
String file = jrExpression.getText();
file = file.substring(1, file.length()-8)+".jrxml";
JasperCompileManager.compileReport(file);
}
}

关于java - Jasper Reports 中的子报表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3067502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com