目前我正在使用 JasperReports 生成pdf,显示付款列表,每页一个实体。代码(对于一个实体):
Map<String, Object> parameters = new HashMap<String, Object>();
JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile(path + "WEB-INF/jasper/PaymentOrder.jasper");
report = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
我需要做的是在一个文件中显示两个enitities。我试图使用子报告来做到这一点。
主报告:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report name" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<title>
<band height="531" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="555" height="20"/>
<textElement verticalAlignment="Middle">
<font fontName="Times New Roman" isUnderline="false" pdfEncoding="UTF-8" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA["* MASTER REPORT *"]]></textFieldExpression>
</textField>
<subreport runToBottom="false">
<reportElement positionType="Float" x="0" y="55" width="555" height="15" isPrintInFirstWholeBand="true" isPrintWhenDetailOverflows="true"/>
<dataSourceExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.get( "subReport1Params" )]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.get( "subReport1" )]]></subreportExpression>
</subreport>
</band>
</title>
</jasperReport>
子报表:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report name" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<title>
<band height="531" splitType="Stretch">
<textField isBlankWhenNull="true">
<reportElement x="0" y="0" width="555" height="20"/>
<textElement verticalAlignment="Middle">
<font fontName="Times New Roman" isUnderline="false" pdfEncoding="UTF-8" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{REPORT_RESOURCE_BUNDLE}.getString( "user" ) + ": "+$P{REPORT_PARAMETERS_MAP}.get( "user" )]]></textFieldExpression>
</textField>
<textField isBlankWhenNull="true">
<reportElement x="0" y="22" width="555" height="22"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Times New Roman" isUnderline="false" pdfEncoding="UTF-8" isPdfEmbedded="true"/>
</textElement>
<textFieldExpression><![CDATA[$P{REPORT_RESOURCE_BUNDLE}.getString( "payment" ) + " № "+$P{REPORT_PARAMETERS_MAP}.get( "docNumber" )+" "+$P{REPORT_RESOURCE_BUNDLE}.getString( "from" ) + " "+$P{REPORT_PARAMETERS_MAP}.get( "date" )]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
java代码:
HashMap<String, Object> parameters = getParametersForPayment(doc1, user, locale);
String path = ((WebApplication) Application.get()).getServletContext().getRealPath("/");
File fileSubReport1 = new File(path + "WEB-INF/jasper/PaymentOrder.jasper");
parameters.put("subReport1", JRLoader.loadObject(fileSubReport1));
File f = new File(path + "WEB-INF/jasper/PaymentMaster.jasper");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(f);
report = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
答案 0 :(得分:4)
您是对的,您可以通过 REPORT_PARAMETERS_MAP 参数或在 subreportParameter 的帮助下传递参数, subreportParameterExpression 强>
您可以将 subreportExpression 与参数一起使用。
如果您将 REPORT_PARAMETERS_MAP 传递给子报告,则无需执行任何其他操作。
你是如此接近解决你的任务。你的错误是:
$P{REPORT_RESOURCE_BUNDLE}
另一个建议是在模板中添加参数声明。在这种情况下,支持报告更容易,它是良好的风格。
主要报告和两个子报告将在示例中使用。要使用的子报表的名称将通过 Java 代码中的参数传递。我将为 en_US 区域设置添加国际化支持。只有参数用于显示数据(没有任何数据源)。
小资源包 master_en_US.properties 正在使用。
user=User:
payment=Payment:
from=From:
如果我们从 Java 代码传递所有参数,有两种方法:我们可以在 jrxml 文件中声明参数,或者我们可以跳过此步骤。正如我所说,我更喜欢模板中的声明变体(第一种情况)。
在第一种情况下,模板将是:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="master" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="master" whenNoDataType="AllSectionsNoDetail">
<parameter name="user" class="java.lang.String" isForPrompting="false"/>
<parameter name="docNumber" class="java.lang.String"/>
<parameter name="date" class="java.lang.String"/>
<parameter name="subReportName" class="java.lang.String">
<defaultValueExpression><![CDATA["subreport_1"]]></defaultValueExpression>
</parameter>
<title>
<band height="241" splitType="Stretch">
<subreport>
<reportElement x="0" y="20" width="326" height="59" uuid="f629d1c8-658f-4ae0-a492-2912a7868e96"/>
<parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA[$P{subReportName} + ".jasper"]]></subreportExpression>
</subreport>
</band>
</title>
</jasperReport>
在第二种情况下:
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="master" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="master" whenNoDataType="AllSectionsNoDetail">
<title>
<band height="241" splitType="Stretch">
<subreport>
<reportElement x="0" y="20" width="326" height="59" uuid="f629d1c8-658f-4ae0-a492-2912a7868e96"/>
<parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.get("subReportName") + ".jasper"]]></subreportExpression>
</subreport>
</band>
</title>
</jasperReport>
一个子报告将带有参数声明块,另一个子报告带有 - 不带。
subreport_1.jrxml :
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="subreport_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" whenNoDataType="AllSectionsNoDetail">
<parameter name="user" class="java.lang.String" isForPrompting="false"/>
<parameter name="docNumber" class="java.lang.String"/>
<parameter name="date" class="java.lang.String"/>
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement x="0" y="10" width="400" height="15" />
<textElement textAlignment="Left"/>
<textFieldExpression><![CDATA[$P{REPORT_RESOURCE_BUNDLE}.getString("user") + " " + $P{user}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="25" width="400" height="15" />
<textElement textAlignment="Left"/>
<textFieldExpression><![CDATA[$P{REPORT_RESOURCE_BUNDLE}.getString("payment") + " № " + $P{docNumber} + " " +
$P{REPORT_RESOURCE_BUNDLE}.getString("from") + " " + $P{date}]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
subreport_2.jrxml :
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="subreport_2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" whenNoDataType="AllSectionsNoDetail">
<title>
<band height="79" splitType="Stretch">
<textField>
<reportElement x="0" y="10" width="400" height="15" />
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$P{REPORT_RESOURCE_BUNDLE}.getString("user") + " " + $P{REPORT_PARAMETERS_MAP}.get("user")]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
String subreportName;
// some code
JasperReport jasperReport;
try (InputStream inputStream = JRLoader.getResourceInputStream("master.jrxml")) {
jasperReport = JasperCompileManager.compileReport(JRXmlLoader.load(inputStream));
}
Map<String, Object> params = new HashMap<>();
params.put(JRParameter.REPORT_LOCALE, Locale.US);
params.put("user", "Bruce Wayne");
params.put("docNumber", "Some #");
params.put("date", "03.06.2010");
if (!isNullOrEmpty(subreportName)) {
params.put("subReportName", subreportName); // the default name in this case will be set in master report (<defaultValueExpression><![CDATA["subreport_1"]]></defaultValueExpression>)
}
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JREmptyDataSource());
我已使用 PDFExporter ,如果是 subreport_1 ,结果将是:
如果 subreport_2 ,结果将为:
<强> 注意: 强>
答案 1 :(得分:0)
在主目录中(父文件)
在子目录中(子文件)
enter code here
在Java文件中 ResourceBundle resourceBundle = ResourceBundle.getBundle(“ application”,new LOCALE(“ en”));
HashMap参数= new HashMap(); parameters.put(“ REPORT_RESOURCE_BUNDLE”,resourceBundle);
JasperFillManager.fillReport(报告,参数,来源);