我正在尝试做一些非常简单的事情,但是在搜索了过去几天后,我找不到答案了,我需要一些帮助。
我要做什么: 我有多个报告,因此A4代表一些风景,有些只是静态文本,有些带有表格,有些带有表格和变量,我想将它们合并在一起。
他们单独工作100%。
试图使带有子报表的主报表不起作用,尝试采用书本方法不起作用,我总是得到Document为空,或者如果我尝试操纵数据查询,我会得到各种各样的怪异东西。
现在,我将尝试使这个示例尽可能简单。
JSON数据:(新数据适配器)
{
"name": "Sample Name",
"people":[
{
"who": "Person 1"
},
{
"who": "Person 2"
},
{
"who": "Person 3"
}
]
}
图书主报告:
<?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="Empty_Book" pageWidth="595" pageHeight="842" sectionType="Part" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="d2716064-8ae4-40cf-a575-33afba400e3a">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter "/>
<property name="net.sf.jasperreports.print.create.bookmarks" value="true"/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<detail>
<part uuid="69d6ca13-26f6-425f-bff9-395c5b9c183b">
<p:subreportPart xmlns:p="http://jasperreports.sourceforge.net/jasperreports/parts" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/parts http://jasperreports.sourceforge.net/xsd/parts.xsd">
<subreportParameter name="REPORT_CONNECTION">
<subreportParameterExpression><![CDATA[$P{REPORT_CONNECTION}]]></subreportParameterExpression>
</subreportParameter>
<subreportExpression><![CDATA["Blank_A4_2.jasper"]]></subreportExpression>
</p:subreportPart>
</part>
<part uuid="055c9f89-88b4-4270-b6ef-addb2eac3e56">
<p:subreportPart xmlns:p="http://jasperreports.sourceforge.net/jasperreports/parts" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/parts http://jasperreports.sourceforge.net/xsd/parts.xsd">
<subreportParameter name="REPORT_CONNECTION">
<subreportParameterExpression><![CDATA[$P{REPORT_CONNECTION}]]></subreportParameterExpression>
</subreportParameter>
<subreportExpression><![CDATA["Blank_A4_Landscape_1.jasper"]]></subreportExpression>
</p:subreportPart>
</part>
</detail>
</jasperReport>
Blank_A4_2.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_2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="bd2bd70c-5a0d-4c3f-b81d-4d5d7731153d">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter "/>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="name"/>
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="150" y="30" width="100" height="30" uuid="eef5f1dc-3d96-4008-8321-ef0be92d42d4"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="50" y="30" width="100" height="30" uuid="cbe6173f-5399-45ec-926b-ff6f48b4fb0c"/>
<text><![CDATA[name]]></text>
</staticText>
</band>
</detail>
</jasperReport>
Blank_A4_Landscape_1.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_Landscape_1" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="62f06d09-42b5-4471-baa5-5aed60cc5fc4">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter "/>
<subDataset name="Dataset1" uuid="eb54ec0d-0c60-44af-8476-e44f40560dee">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="New Data Adapter "/>
<queryString language="json">
<![CDATA[people]]>
</queryString>
<field name="who" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="who"/>
<fieldDescription><![CDATA[who]]></fieldDescription>
</field>
</subDataset>
<queryString language="JSON">
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="name"/>
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<background>
<band splitType="Stretch"/>
</background>
<columnHeader>
<band height="175">
<textField>
<reportElement x="132" y="81" width="100" height="30" uuid="5cce1f44-2a8b-42ad-a03d-4c294f02d31c"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</band>
</columnHeader>
<detail>
<band height="140" splitType="Stretch">
<componentElement>
<reportElement x="110" y="45" width="200" height="40" uuid="75a06812-f9a0-4b0f-9a08-efec60d0b555"/>
<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
<datasetRun subDataset="Dataset1" uuid="acd62160-c07c-4c00-b6b4-9ef001d1585d">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("people")]]></dataSourceExpression>
</datasetRun>
<jr:column width="200" uuid="253a610c-a780-4690-8cce-8a03597ef016">
<jr:detailCell height="30">
<textField>
<reportElement x="0" y="0" width="200" height="30" uuid="910a7e24-c02b-4b21-8c9d-6a3dfda21794"/>
<textFieldExpression><![CDATA[$F{who}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>
在发布之前测试了所有文件,它们可以单独工作,我没有参数可以发送到子报表,我尝试循环遍历数组,无论我做什么,我总是得到的文档都是空的。
答案 0 :(得分:1)
将REPORT_CONNECTION
传递到每个子报告部分均无效。仅对于JDBC连接,其值将为非null。因此,没有理由通过它。
可以通过将每个子报表绑定到数据适配器文件来运行设置:
将此属性添加到每个子报表中:
<property name="net.sf.jasperreports.data.adapter" value="JSON_DA.xml"/>
(也可以通过在“报告属性”标签中设置“默认数据适配器”来完成此操作)
重新编译每个子报表,然后再次预览主报表。
所有这些都是必要的,因为JasperSoft Studio不会在运行时将数据适配器注入到每个子报表中。仅当通过自动设置com.jaspersoft.studio.data.defaultdataadapter
属性来单独运行(预览)每个报告时,才会注入它。