我正在努力使用Java生成JasperReport。没有考虑数据源,因此所有值均为null,并且未显示subReport。
我已经从下面粘贴的JSON中使用选项“在填充报告时使用报告JSON表达式”创建了JSONDataAdapter。从Jasper Studio进行编译时,它可以正确创建报告,而尝试从Java进行编译时,将导致报告完全为空(仅显示字段名称),而根本没有subReport。 请帮助解决此问题。
{
"user": {
"firstName": "Mike",
"lastName": "Jones",
"email": "mikeJones@gmail.com",
"userStatus": "VALIDATED",
"documentList": [
{
"id": 2,
"documentType": "DRIVING_LICENSE",
"documentStatus": "APPROVED",
"comment": "",
"events": [
{
"action": "APPROVE",
"comment": "",
"createDate": "2018-05-24T10:44:55.987+0000"
}
]
},
{
"id": 1,
"documentType": "PASSPORT",
"documentStatus": "APPROVED",
"comment": "",
"events": [
{
"action": "APPROVE",
"comment": "",
"createDate": "2018-05-24T10:44:55.901+0000"
}
]
}
]
}
}
main.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="Blank_A4_7" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ed8211ff-fccf-451c-9a4e-0b2075ebdfee">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="DataAdapter1.xml"/>
<queryString language="json">
<![CDATA[user]]>
</queryString>
<field name="firstName" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="firstName"/>
<fieldDescription><![CDATA[firstName]]></fieldDescription>
</field>
<field name="lastName" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="lastName"/>
<fieldDescription><![CDATA[lastName]]></fieldDescription>
</field>
<field name="email" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="email"/>
<fieldDescription><![CDATA[email]]></fieldDescription>
</field>
<field name="userStatus" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="userStatus"/>
<fieldDescription><![CDATA[userStatus]]></fieldDescription>
</field>
<field name="documentList" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="documentList"/>
<fieldDescription><![CDATA[documentList]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="35" splitType="Stretch">
<staticText>
<reportElement x="-7" y="0" width="100" height="30" uuid="85f69194-56d8-413f-a550-1884c6c2ffdd">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="03de927f-90a6-4a3b-b435-78f511e83566"/>
</reportElement>
<text><![CDATA[firstName]]></text>
</staticText>
<staticText>
<reportElement x="99" y="0" width="100" height="30" uuid="97a45c99-e11f-466e-83a9-0ee3125a888a">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="e2dcce70-e78a-454f-897b-5961f5b060d7"/>
</reportElement>
<text><![CDATA[lastName]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="277" splitType="Stretch">
<textField>
<reportElement x="-10" y="0" width="100" height="30" uuid="d2158da3-7029-46b7-87e5-73dff198aa5c">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="03de927f-90a6-4a3b-b435-78f511e83566"/>
</reportElement>
<textFieldExpression><![CDATA[$F{firstName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="30" uuid="ffe6c4e3-0584-4ff0-bb85-8bc1c586424f">
<property name="com.jaspersoft.studio.spreadsheet.connectionID" value="e2dcce70-e78a-454f-897b-5961f5b060d7"/>
</reportElement>
<textFieldExpression><![CDATA[$F{lastName}]]></textFieldExpression>
</textField>
<subreport>
<reportElement x="-7" y="30" width="557" height="60" uuid="05a4dede-97de-4836-9195-114755ff921a"/>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("documentList")]]></dataSourceExpression>
<subreportExpression><![CDATA["subreport.jasper"]]></subreportExpression>
</subreport>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
subreport.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0 -->
<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="Blank_A4_4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="f841d01d-b1ed-4398-9c3a-85751cabd974">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="DataAdapter1.xml"/>
<queryString language="json">
<![CDATA[user.documentList]]>
</queryString>
<field name="id" class="java.lang.Integer">
<property name="net.sf.jasperreports.json.field.expression" value="id"/>
<fieldDescription><![CDATA[id]]></fieldDescription>
</field>
<field name="documentType" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="documentType"/>
<fieldDescription><![CDATA[documentType]]></fieldDescription>
</field>
<field name="documentStatus" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="documentStatus"/>
<fieldDescription><![CDATA[documentStatus]]></fieldDescription>
</field>
<field name="comment" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="comment"/>
<fieldDescription><![CDATA[comment]]></fieldDescription>
</field>
<field name="events" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="events"/>
<fieldDescription><![CDATA[events]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="36" splitType="Stretch">
<textField>
<reportElement x="-7" y="5" width="100" height="30" uuid="b904bfb3-ac61-4141-bbd4-813aa6101945"/>
<textFieldExpression><![CDATA[$F{documentType}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="120" y="3" width="100" height="30" uuid="85c25658-8388-4eb0-ae5b-b376ce801d0f"/>
<textFieldExpression><![CDATA[$F{documentStatus}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Test.java
public class Test {
public static void main(String[] args) {
try {
String pathToReports = "/home/test/";
Map<String, Object> params = new HashMap<>();
params.put(JsonQueryExecuterFactory.JSON_DATE_PATTERN, "yyyy-MM-dd");
params.put(JsonQueryExecuterFactory.JSON_NUMBER_PATTERN, "#,##0.##");
params.put(JsonQueryExecuterFactory.JSON_LOCALE, Locale.ENGLISH);
params.put(JRParameter.REPORT_LOCALE, Locale.US);
JRDataSource jsonDataSource = new JsonDataSource(new File("/home/test/example.json"));
JasperReport jasperReport = JasperCompileManager.compileReport(pathToReports + "main.jrxml");
JasperCompileManager.compileReportToFile(pathToReports + "subreport.jrxml", "subreport.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, jsonDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "/home/test/test.pdf");
} catch (Exception e) {}
}
}
答案 0 :(得分:1)
从Java代码运行时,您的报告为空,因为您没有默认数据适配器。它与Jaspersoft Studio的不同。要么设置它:
或直接使用报告级别属性:
<property name="net.sf.jasperreports.data.adapter" value="DataAdapter1.xml"/>