【问题标题】:How to print a jasper report in 3 exemplar with little changes?如何以 3 个示例打印碧玉报告而几乎没有什么变化?
【发布时间】:2013-06-22 04:47:28
【问题描述】:

我已经用 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 jasper-reports ireport


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您可以使用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 LabelPRINT_FOR等于Client时输出Client Label,如果不等于任何一个则输出Original Label以上。

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

      Map<String, Object> parameters = new HashMap<String, Object>();
      parameters.put("PRINT_FOR", "CLIENT");
      


      更新:根据您的评论,您基本上希望同时将所有 3 个报告导出为一个。这可以通过使用JRPrintServiceExporter 来实现。基本上创建三个JasperPrint 对象,并将它们放在一个列表中。然后使用导出器将它们打印出来。就像是:
      //add all three JasperPrints to the list below
      List<JasperPrint> jasperPrints = new ArrayList<JasperPrint>();
      
      ...
      
      //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();
      

      【讨论】:

      • 感谢您的评论。这段代码每次只打印一个示例。我想要的是每次用户单击我的应用程序中的打印按钮时打印 3 个示例。
      • @itro 抱歉,我没有早点更新,我只是没有机会在此之前输入任何内容。希望对您有所帮助。
      【解决方案3】:

      当您使用自己的 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-05
        • 1970-01-01
        • 1970-01-01
        • 2011-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多