您可以使用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时输出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();