使用 JsonDataSource 在子报表中使用子报表进行报表
Report with subreport in subreport using JsonDataSource
我有 3 个 .jrxml(主报表、子报表和子报表中的子报表)并从 JSON 获取数据。我发送的子报告 jasper 文件的路径类似于参数(第一个子报告 - sub_jasper_report,第二个子报告 - sub_jasper_report2)
JSON
{
"name1": "",
"joints": [
{
"name2": "1",
"zones": [
{
"name3": "1"
},
{
"name3": "2"
}
]
},
{
"name2": "2",
"zones": [
{
"name3": "1"
},
{
"name3": "2"
}
]
}
]
}
MAIN REPORT 中的子报表(我在子报表中发送数组接头并且它有效)
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("joints")]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{sub_jasper_report}]]></subreportExpression>
在第一个子报表字段中,“名称”工作正常。但是第一个子报表也有子报表(区域),我也尝试发送。
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("joints.zones")]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{sub_jasper_report2}]]></subreportExpression>
但是在第二个子报表(子报表的子报表)中填写“名称”不起作用。
看来我不仅要在主报表中而且还要在第一个子报表中发送参数(.jrxl 的路径)。但是我不知道怎么办。
主报表
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.16.0.final using JasperReports Library version 6.16.0-48579d909b7943b64690c65c71e07e0b80981928 -->
<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="RK_GOST_RUS" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="758" leftMargin="56" rightMargin="28" topMargin="28" bottomMargin="28" uuid="2fecaf74-39aa-42a2-9e1d-4ef078bb2bc2">
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
<parameter name="sub_jasper_report" class="net.sf.jasperreports.engine.JasperReport" isForPrompting="false">
<parameterDescription><![CDATA[Subreport]]></parameterDescription>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<field name="A9" class="java.lang.String">
<fieldDescription><![CDATA[name1]]></fieldDescription>
</field>
<detail>
<band height="10" splitType="Stretch">
<subreport>
<reportElement x="0" y="0" width="758" height="10" uuid="74977f92-bc0b-4e05-8335-a22d78db9898">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("joints")]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{sub_jasper_report}]]></subreportExpression>
</subreport>
<textField textAdjust="ScaleFont">
<reportElement stretchType="ContainerHeight" x="350" y="114" width="408" height="22" uuid="6aec8b48-d3b7-443f-8ac7-3ee2fa611aee">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box padding="1">
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Liberation Serif" size="7" isBold="true" isUnderline="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{A9}]]></textFieldExpression>
</textField>
</band>
<band height="119">
</detail>
</jasperReport>
第一个子报表
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.16.0.final using JasperReports Library version 6.16.0-48579d909b7943b64690c65c71e07e0b80981928 -->
<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="RK_GOST_RUS_SUB1" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="758" leftMargin="56" rightMargin="28" topMargin="28" bottomMargin="28" uuid="2fecaf74-39aa-42a2-9e1d-4ef078bb2bc2">
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
<parameter name="sub_jasper_report" class="net.sf.jasperreports.engine.JasperReport" isForPrompting="false">
<parameterDescription><![CDATA[Subreport]]></parameterDescription>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<field name="A9" class="java.lang.String">
<fieldDescription><![CDATA[name2]]></fieldDescription>
</field>
<detail>
<band height="10" splitType="Stretch">
<subreport>
<reportElement x="0" y="0" width="758" height="10" uuid="74977f92-bc0b-4e05-8335-a22d78db9898">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("zones")]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{sub_jasper_report}]]></subreportExpression>
</subreport>
<textField textAdjust="ScaleFont">
<reportElement stretchType="ContainerHeight" x="350" y="114" width="408" height="22" uuid="6aec8b48-d3b7-443f-8ac7-3ee2fa611aee">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box padding="1">
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Liberation Serif" size="7" isBold="true" isUnderline="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{A9}]]></textFieldExpression>
</textField>
</band>
<band height="119">
</detail>
</jasperReport>
第二个子报表
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.16.0.final using JasperReports Library version 6.16.0-48579d909b7943b64690c65c71e07e0b80981928 -->
<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="RK_GOST_RUS_SUB1" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="758" leftMargin="56" rightMargin="28" topMargin="28" bottomMargin="28" uuid="2fecaf74-39aa-42a2-9e1d-4ef078bb2bc2">
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="A9" class="java.lang.String">
<fieldDescription><![CDATA[name3]]></fieldDescription>
</field>
<detail>
<band height="10" splitType="Stretch">
<textField textAdjust="ScaleFont">
<reportElement stretchType="ContainerHeight" x="350" y="114" width="408" height="22" uuid="6aec8b48-d3b7-443f-8ac7-3ee2fa611aee">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box padding="1">
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Liberation Serif" size="7" isBold="true" isUnderline="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{A9}]]></textFieldExpression>
</textField>
</band>
<band height="119">
</detail>
</jasperReport>
程序中的函数,对一个子报表有效,但对子报表中的子报表无效。我通过字段描述从 json 获取数据,并将 F(字段)放在“文本字段”中。
fun createJasper(
jasperName: String,
parameter: Map<String, Any>,
json: ByteArray,
): JasperPrint? {
val jasperReport = JRLoader.loadObject(javaClass.classLoader.getResourceAsStream(jasperName)) as JasperReport
return JasperFillManager.fillReport(
jasperReport,
parameter, // Path to Subreports
if (json.isEmpty()) JREmptyDataSource() else JsonDataSource(ByteArrayInputStream(json))
)
}
哦,我只是需要将参数从主报表发送到子报表。并且仍然只使用参数
<subreportParameter name="sub_jasper_report">
<subreportParameterExpression><![CDATA[$P{sub_jasper_report}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="sub_jasper_report2">
<subreportParameterExpression><![CDATA[$P{sub_jasper_report2}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="sub_jasper_report3">
<subreportParameterExpression><![CDATA[$P{sub_jasper_report3}]]></subreportParameterExpression>
</subreportParameter>
我有 3 个 .jrxml(主报表、子报表和子报表中的子报表)并从 JSON 获取数据。我发送的子报告 jasper 文件的路径类似于参数(第一个子报告 - sub_jasper_report,第二个子报告 - sub_jasper_report2)
JSON
{
"name1": "",
"joints": [
{
"name2": "1",
"zones": [
{
"name3": "1"
},
{
"name3": "2"
}
]
},
{
"name2": "2",
"zones": [
{
"name3": "1"
},
{
"name3": "2"
}
]
}
]
}
MAIN REPORT 中的子报表(我在子报表中发送数组接头并且它有效)
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("joints")]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{sub_jasper_report}]]></subreportExpression>
在第一个子报表字段中,“名称”工作正常。但是第一个子报表也有子报表(区域),我也尝试发送。
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("joints.zones")]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{sub_jasper_report2}]]></subreportExpression>
但是在第二个子报表(子报表的子报表)中填写“名称”不起作用。
看来我不仅要在主报表中而且还要在第一个子报表中发送参数(.jrxl 的路径)。但是我不知道怎么办。
主报表
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.16.0.final using JasperReports Library version 6.16.0-48579d909b7943b64690c65c71e07e0b80981928 -->
<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="RK_GOST_RUS" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="758" leftMargin="56" rightMargin="28" topMargin="28" bottomMargin="28" uuid="2fecaf74-39aa-42a2-9e1d-4ef078bb2bc2">
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
<parameter name="sub_jasper_report" class="net.sf.jasperreports.engine.JasperReport" isForPrompting="false">
<parameterDescription><![CDATA[Subreport]]></parameterDescription>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<field name="A9" class="java.lang.String">
<fieldDescription><![CDATA[name1]]></fieldDescription>
</field>
<detail>
<band height="10" splitType="Stretch">
<subreport>
<reportElement x="0" y="0" width="758" height="10" uuid="74977f92-bc0b-4e05-8335-a22d78db9898">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("joints")]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{sub_jasper_report}]]></subreportExpression>
</subreport>
<textField textAdjust="ScaleFont">
<reportElement stretchType="ContainerHeight" x="350" y="114" width="408" height="22" uuid="6aec8b48-d3b7-443f-8ac7-3ee2fa611aee">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box padding="1">
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Liberation Serif" size="7" isBold="true" isUnderline="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{A9}]]></textFieldExpression>
</textField>
</band>
<band height="119">
</detail>
</jasperReport>
第一个子报表
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.16.0.final using JasperReports Library version 6.16.0-48579d909b7943b64690c65c71e07e0b80981928 -->
<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="RK_GOST_RUS_SUB1" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="758" leftMargin="56" rightMargin="28" topMargin="28" bottomMargin="28" uuid="2fecaf74-39aa-42a2-9e1d-4ef078bb2bc2">
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
<parameter name="sub_jasper_report" class="net.sf.jasperreports.engine.JasperReport" isForPrompting="false">
<parameterDescription><![CDATA[Subreport]]></parameterDescription>
</parameter>
<queryString>
<![CDATA[]]>
</queryString>
<field name="A9" class="java.lang.String">
<fieldDescription><![CDATA[name2]]></fieldDescription>
</field>
<detail>
<band height="10" splitType="Stretch">
<subreport>
<reportElement x="0" y="0" width="758" height="10" uuid="74977f92-bc0b-4e05-8335-a22d78db9898">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource) $P{REPORT_DATA_SOURCE}).subDataSource("zones")]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{sub_jasper_report}]]></subreportExpression>
</subreport>
<textField textAdjust="ScaleFont">
<reportElement stretchType="ContainerHeight" x="350" y="114" width="408" height="22" uuid="6aec8b48-d3b7-443f-8ac7-3ee2fa611aee">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box padding="1">
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Liberation Serif" size="7" isBold="true" isUnderline="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{A9}]]></textFieldExpression>
</textField>
</band>
<band height="119">
</detail>
</jasperReport>
第二个子报表
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.16.0.final using JasperReports Library version 6.16.0-48579d909b7943b64690c65c71e07e0b80981928 -->
<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="RK_GOST_RUS_SUB1" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="758" leftMargin="56" rightMargin="28" topMargin="28" bottomMargin="28" uuid="2fecaf74-39aa-42a2-9e1d-4ef078bb2bc2">
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="A9" class="java.lang.String">
<fieldDescription><![CDATA[name3]]></fieldDescription>
</field>
<detail>
<band height="10" splitType="Stretch">
<textField textAdjust="ScaleFont">
<reportElement stretchType="ContainerHeight" x="350" y="114" width="408" height="22" uuid="6aec8b48-d3b7-443f-8ac7-3ee2fa611aee">
<property name="com.jaspersoft.studio.unit.width" value="px"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<box padding="1">
<bottomPen lineWidth="0.5"/>
<rightPen lineWidth="0.5"/>
</box>
<textElement textAlignment="Left" verticalAlignment="Middle">
<font fontName="Liberation Serif" size="7" isBold="true" isUnderline="true"/>
</textElement>
<textFieldExpression><![CDATA[$F{A9}]]></textFieldExpression>
</textField>
</band>
<band height="119">
</detail>
</jasperReport>
程序中的函数,对一个子报表有效,但对子报表中的子报表无效。我通过字段描述从 json 获取数据,并将 F(字段)放在“文本字段”中。
fun createJasper(
jasperName: String,
parameter: Map<String, Any>,
json: ByteArray,
): JasperPrint? {
val jasperReport = JRLoader.loadObject(javaClass.classLoader.getResourceAsStream(jasperName)) as JasperReport
return JasperFillManager.fillReport(
jasperReport,
parameter, // Path to Subreports
if (json.isEmpty()) JREmptyDataSource() else JsonDataSource(ByteArrayInputStream(json))
)
}
哦,我只是需要将参数从主报表发送到子报表。并且仍然只使用参数
<subreportParameter name="sub_jasper_report">
<subreportParameterExpression><![CDATA[$P{sub_jasper_report}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="sub_jasper_report2">
<subreportParameterExpression><![CDATA[$P{sub_jasper_report2}]]></subreportParameterExpression>
</subreportParameter>
<subreportParameter name="sub_jasper_report3">
<subreportParameterExpression><![CDATA[$P{sub_jasper_report3}]]></subreportParameterExpression>
</subreportParameter>