如何以3个样本打印碧玉报告,几乎没有变化?

我用iReport创建了一个jasper报告,我可以完美地打印出来。 我需要打印3个示例(原始示例,客户端示例,部门示例),只需更改一些,例如更改报表中的标签。

我将PRINT_FOR作为参数传递给iReport。

有没有人知道如何实现这一目标?

 HashMap parameters = new HashMap(); String option = "C:\\option.jasper"; JRDataSource beanDataSource = reportMapper.getDataSource(); JasperPrint jasperPrint = JasperFillManager.fillReport(option, parameters, beanDataSource); JasperPrintManager.printPage(jasperPrint, 0, true)) 

首先想到的是制作一个通用的报告模板,并有一些“钩子”,你可以在其中插入每个版本的差异; 您可以通过Java中的参数发送“差异”。

您可以使用允许使用和表达式确定文本输出的Text Field ,而不是使用Static Text字段。 在这种情况下,您将检查PRINT_FOR参数是否等于客户端或部门,如果不使用原始值。 你的表达式看起来像这样:

 ($P{PRINT_FOR}.equals("DEPARTMENT") ? "Department Label" : ($P{PRINT_FOR}.equals("CLIENT") ? "Client Label" : "Original Label")) 

PRINT_FOR等于DEPARMTNENT时输出Department Label ,当PRINT_FOR等于Client Label时输出Client Label ,如果不等于上述任何一个,则输出Original Label

另外值得注意的是,在代码片段中,您从未在Java代码中设置PRINT_FOR参数的值,并且您没有使用通用HashMap 。 它应该看起来更接近:

 Map parameters = new HashMap(); parameters.put("PRINT_FOR", "CLIENT"); 

更新:根据您的评论,您基本上希望同时将所有3个报告导出为一个。 这可以通过使用JRPrintServiceExporter来完成。 基本上创建三个JasperPrint对象,并将它们放在一个列表中。 然后使用导出器将其打印出来。 就像是:

 //add all three JasperPrints to the list below List jasperPrints = new ArrayList(); ... //create an exporter JRExporter exporter = new JRPrintServiceExporter(); //add the JasperPrints to the exporter via the JASPER_PRINT_LIST param exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrints); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE); //this one makes it so that the settings choosen in the first dialog will be applied to the //other documents in the list also exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG_ONLY_ONCE, Boolean.TRUE); exporter.exportReport(); 

当您使用自己的JRBeanCollectionDataSource时,必须为每个JasperPrint创建各自的JRBeanCollectionDataSource

  JRBeanCollectionDataSource dsCliente = new JRBeanCollectionDataSource(listaDetalle); JasperPrint jasperPrintCliente = JasperFillManager.fillReport("plantillaDoc.jasper", paramsHojaCliente, dsCliente); listaJasperPrint.add(jasperPrintCliente); JRBeanCollectionDataSource dsCaja1 = new JRBeanCollectionDataSource(listaDetalle); JasperPrint jasperPrintCaja1 = JasperFillManager.fillReport("plantillaDoc.jasper", paramsHojaCaja, dsCaja1); listaJasperPrint.add(jasperPrintCaja1);