如何在 Jasper Studio 中使用 table 和 table 的返回值计算总和?
How to calculate a sum with a table and a returned value from table in Jasper Studio?
我有一个 table 和一个小组的报告。我需要报告的列脚注使用 table 的变量 retuned 求和。可能吗?
我举了个例子。在此示例中,我希望变量 "sumItemValueFromTable" 显示值 10000.0。目前,结果是 0.0.
这是报告的 JRXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.0.final using JasperReports Library version 6.4.1 -->
<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="example_report" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isFloatColumnFooter="true" uuid="98b654fa-866e-4db4-9a3d-372b1e6e06c7">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="datasouce"/>
<style name="Table_TH" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_CH" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
<style backcolor="#FBFDFF"/>
</conditionalStyle>
</style>
<subDataset name="items" uuid="940ee07b-57c1-48bd-bbfe-9ee2ac66d304">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="datasouce"/>
<queryString language="json">
<![CDATA[items]]>
</queryString>
<field name="itemName" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="itemName"/>
<fieldDescription><![CDATA[itemName]]></fieldDescription>
</field>
<field name="itemValue" class="java.lang.Double">
<property name="net.sf.jasperreports.json.field.expression" value="itemValue"/>
<fieldDescription><![CDATA[itemValue]]></fieldDescription>
</field>
<variable name="sumItem" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{itemValue}]]></variableExpression>
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
</subDataset>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="userName" class="java.lang.String">
<fieldDescription><![CDATA[userName]]></fieldDescription>
</field>
<field name="userDescription" class="java.lang.String">
<fieldDescription><![CDATA[userDescription]]></fieldDescription>
</field>
<variable name="sumItemValueGeneral" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$V{sumItemValueFromTable}]]></variableExpression>
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
<variable name="sumItemValueFromTable" class="java.lang.Double" resetType="Group" resetGroup="user">
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
<group name="user">
<groupExpression><![CDATA[$F{userName}]]></groupExpression>
<groupHeader>
<band height="15">
<textField>
<reportElement x="80" y="0" width="720" height="15" uuid="4169f25b-4f54-4aa1-9621-38d9d26fba36"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{userName} + " - " + $F{userDescription}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="80" height="15" uuid="6764c7d6-fd26-4057-8120-63f4b55f3100">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="9" isBold="true"/>
</textElement>
<text><![CDATA[User:]]></text>
</staticText>
</band>
</groupHeader>
<groupFooter>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="180" height="15" uuid="b61688ee-94d6-4c74-aa6f-035b7bbd6dbd">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="9" isBold="true"/>
</textElement>
<text><![CDATA[USER SUM]]></text>
</staticText>
<textField>
<reportElement x="180" y="0" width="100" height="14" uuid="919abbde-42c3-4503-bcc8-ad68ffac7950"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{sumItemValueFromTable}]]></textFieldExpression>
</textField>
</band>
</groupFooter>
</group>
<title>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="800" height="15" uuid="b71231cd-1292-4cad-96fb-918dc371bf78">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[My report]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="17" splitType="Stretch">
<staticText>
<reportElement x="0" y="2" width="800" height="15" uuid="5f05f44c-eabb-4aaf-b3c2-6c2512f60f73">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[A page header]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="80" y="0" width="100" height="15" uuid="f4a14a24-b4cb-46d4-a799-f7401178d6b7">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="180" y="0" width="100" height="15" uuid="63b605a9-d94e-449f-b4e8-c5b5fa2352fe">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[Value]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="15" splitType="Stretch">
<componentElement>
<reportElement x="80" y="0" width="200" height="15" uuid="36dcd5f4-e9bd-4577-8ff4-dceacfa42263">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
<property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
<property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<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="items" uuid="98564efc-611d-4267-b650-20b8ce408049">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("items")]]></dataSourceExpression>
<returnValue fromVariable="sumItem" toVariable="sumItemValueFromTable"/>
</datasetRun>
<jr:column width="100" uuid="594a2c22-50a2-4cb3-bd1b-5b73edb3e8c5">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<jr:detailCell style="Table_TD" height="15">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textField>
<reportElement x="0" y="0" width="100" height="15" uuid="466d9b29-f34d-4718-8f33-47019eaac2f4">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{itemName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="5a0cc5e4-46b3-463a-8e5e-b14f2444559f">
<jr:detailCell style="Table_TD" height="15">
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textField>
<reportElement x="0" y="0" width="100" height="15" uuid="404014b5-2c4b-4479-8a61-517700f7bdd2"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{itemValue}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
<columnFooter>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="180" height="15" uuid="b27a2374-0c07-429c-820f-d44a52faf5b8">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="9" isBold="true"/>
</textElement>
<text><![CDATA[GENERAL SUM]]></text>
</staticText>
<textField evaluationTime="Band">
<reportElement x="180" y="-1" width="100" height="16" uuid="34659bd7-0bb3-4001-9767-91489dc96329"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{sumItemValueGeneral}]]></textFieldExpression>
</textField>
</band>
</columnFooter>
</jasperReport>
这是我使用的 JSON 数据源:
[
{
"userName": "User A",
"userDescription": "Description A",
"items": [
{
"itemName": "Item A",
"itemValue": 100.0
},
{
"itemName": "Item B",
"itemValue": 200.0
}
]
},
{
"userName": "User B",
"userDescription": "Description B",
"items": [
{
"itemName": "Item A",
"itemValue": 400.0
},
{
"itemName": "Item B",
"itemValue": 300.0
}
]
}
]
按照你的代码布局,一个解决方案是在你的 table 的子数据集中定义另一个类似于 sumItem
变量的变量,因为你只能在 JasperSoft 中为每个变量定义 1 return 值Studio(版本 <=6.6.0 有这个奇怪的错误),如果您直接编辑 jrxml,则可以避免此步骤,而是使用 returnValue.
中的先前变量
<variable name="sumItemForTotale" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{itemValue}]]></variableExpression>
</variable>
然后return这个到变量sumItemValueGeneral
计算Sum
<returnValue fromVariable="sumItemForTotale" toVariable="sumItemValueGeneral" calculation="Sum"/>
现在删除变量 sumItemValueGeneral
中的当前表达式并将其设置为 Sum
<variable name="sumItemValueGeneral" class="java.lang.Double" calculation="Sum">
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
是的,您需要同时设置变量和 returnValue 来计算 Sum
我个人觉得有点混乱,只将变量设置为 [= 感觉更合乎逻辑16=],但我猜逻辑是:如果你不在 returnValue 中设置 Sum
(值将被强制复制,因为它直接进入变量)并且如果你不设置 Sum
in variabile,当它得到新的'add'值时,变量将被重新初始化。
完整 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="example_report" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isFloatColumnFooter="true" uuid="98b654fa-866e-4db4-9a3d-372b1e6e06c7">
<style name="Table_TH" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
<style backcolor="#FBFDFF"/>
</conditionalStyle>
</style>
<style name="Table_CH" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="items" uuid="940ee07b-57c1-48bd-bbfe-9ee2ac66d304">
<queryString language="json">
<![CDATA[items]]>
</queryString>
<field name="itemName" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="itemName"/>
<fieldDescription><![CDATA[itemName]]></fieldDescription>
</field>
<field name="itemValue" class="java.lang.Double">
<property name="net.sf.jasperreports.json.field.expression" value="itemValue"/>
<fieldDescription><![CDATA[itemValue]]></fieldDescription>
</field>
<variable name="sumItem" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{itemValue}]]></variableExpression>
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
<variable name="sumItemForTotale" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{itemValue}]]></variableExpression>
</variable>
</subDataset>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="userName" class="java.lang.String">
<fieldDescription><![CDATA[userName]]></fieldDescription>
</field>
<field name="userDescription" class="java.lang.String">
<fieldDescription><![CDATA[userDescription]]></fieldDescription>
</field>
<variable name="sumItemValueGeneral" class="java.lang.Double" calculation="Sum">
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
<variable name="sumItemValueFromTable" class="java.lang.Double" resetType="Group" resetGroup="user">
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
<group name="user">
<groupExpression><![CDATA[$F{userName}]]></groupExpression>
<groupHeader>
<band height="15">
<textField>
<reportElement x="80" y="0" width="720" height="15" uuid="4169f25b-4f54-4aa1-9621-38d9d26fba36"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{userName} + " - " + $F{userDescription}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="80" height="15" uuid="6764c7d6-fd26-4057-8120-63f4b55f3100"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="9" isBold="true"/>
</textElement>
<text><![CDATA[User:]]></text>
</staticText>
</band>
</groupHeader>
<groupFooter>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="180" height="15" uuid="b61688ee-94d6-4c74-aa6f-035b7bbd6dbd"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="9" isBold="true"/>
</textElement>
<text><![CDATA[USER SUM]]></text>
</staticText>
<textField>
<reportElement x="180" y="0" width="100" height="14" uuid="919abbde-42c3-4503-bcc8-ad68ffac7950"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{sumItemValueFromTable}]]></textFieldExpression>
</textField>
</band>
</groupFooter>
</group>
<title>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="800" height="15" uuid="b71231cd-1292-4cad-96fb-918dc371bf78"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[My report]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="17" splitType="Stretch">
<staticText>
<reportElement x="0" y="2" width="800" height="15" uuid="5f05f44c-eabb-4aaf-b3c2-6c2512f60f73"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[A page header]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="80" y="0" width="100" height="15" uuid="f4a14a24-b4cb-46d4-a799-f7401178d6b7"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="180" y="0" width="100" height="15" uuid="63b605a9-d94e-449f-b4e8-c5b5fa2352fe"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[Value]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="15" splitType="Stretch">
<componentElement>
<reportElement x="80" y="0" width="200" height="15" uuid="36dcd5f4-e9bd-4577-8ff4-dceacfa42263"/>
<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="items" uuid="98564efc-611d-4267-b650-20b8ce408049">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("items")]]></dataSourceExpression>
<returnValue fromVariable="sumItem" toVariable="sumItemValueFromTable"/>
<returnValue fromVariable="sumItemForTotale" toVariable="sumItemValueGeneral" calculation="Sum"/>
</datasetRun>
<jr:column width="100" uuid="594a2c22-50a2-4cb3-bd1b-5b73edb3e8c5">
<jr:detailCell style="Table_TD" height="15">
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textField>
<reportElement x="0" y="0" width="100" height="15" uuid="466d9b29-f34d-4718-8f33-47019eaac2f4"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{itemName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="5a0cc5e4-46b3-463a-8e5e-b14f2444559f">
<jr:detailCell style="Table_TD" height="15">
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textField>
<reportElement x="0" y="0" width="100" height="15" uuid="404014b5-2c4b-4479-8a61-517700f7bdd2"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{itemValue}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
<columnFooter>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="180" height="15" uuid="b27a2374-0c07-429c-820f-d44a52faf5b8"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="9" isBold="true"/>
</textElement>
<text><![CDATA[GENERAL SUM]]></text>
</staticText>
<textField evaluationTime="Report">
<reportElement x="180" y="-1" width="100" height="16" uuid="34659bd7-0bb3-4001-9767-91489dc96329"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{sumItemValueGeneral}]]></textFieldExpression>
</textField>
</band>
</columnFooter>
</jasperReport>
输出
我有一个 table 和一个小组的报告。我需要报告的列脚注使用 table 的变量 retuned 求和。可能吗?
我举了个例子。在此示例中,我希望变量 "sumItemValueFromTable" 显示值 10000.0。目前,结果是 0.0.
这是报告的 JRXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.0.final using JasperReports Library version 6.4.1 -->
<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="example_report" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isFloatColumnFooter="true" uuid="98b654fa-866e-4db4-9a3d-372b1e6e06c7">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="datasouce"/>
<style name="Table_TH" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_CH" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
<style backcolor="#FBFDFF"/>
</conditionalStyle>
</style>
<subDataset name="items" uuid="940ee07b-57c1-48bd-bbfe-9ee2ac66d304">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="datasouce"/>
<queryString language="json">
<![CDATA[items]]>
</queryString>
<field name="itemName" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="itemName"/>
<fieldDescription><![CDATA[itemName]]></fieldDescription>
</field>
<field name="itemValue" class="java.lang.Double">
<property name="net.sf.jasperreports.json.field.expression" value="itemValue"/>
<fieldDescription><![CDATA[itemValue]]></fieldDescription>
</field>
<variable name="sumItem" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{itemValue}]]></variableExpression>
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
</subDataset>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="userName" class="java.lang.String">
<fieldDescription><![CDATA[userName]]></fieldDescription>
</field>
<field name="userDescription" class="java.lang.String">
<fieldDescription><![CDATA[userDescription]]></fieldDescription>
</field>
<variable name="sumItemValueGeneral" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$V{sumItemValueFromTable}]]></variableExpression>
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
<variable name="sumItemValueFromTable" class="java.lang.Double" resetType="Group" resetGroup="user">
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
<group name="user">
<groupExpression><![CDATA[$F{userName}]]></groupExpression>
<groupHeader>
<band height="15">
<textField>
<reportElement x="80" y="0" width="720" height="15" uuid="4169f25b-4f54-4aa1-9621-38d9d26fba36"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{userName} + " - " + $F{userDescription}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="80" height="15" uuid="6764c7d6-fd26-4057-8120-63f4b55f3100">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="9" isBold="true"/>
</textElement>
<text><![CDATA[User:]]></text>
</staticText>
</band>
</groupHeader>
<groupFooter>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="180" height="15" uuid="b61688ee-94d6-4c74-aa6f-035b7bbd6dbd">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="9" isBold="true"/>
</textElement>
<text><![CDATA[USER SUM]]></text>
</staticText>
<textField>
<reportElement x="180" y="0" width="100" height="14" uuid="919abbde-42c3-4503-bcc8-ad68ffac7950"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{sumItemValueFromTable}]]></textFieldExpression>
</textField>
</band>
</groupFooter>
</group>
<title>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="800" height="15" uuid="b71231cd-1292-4cad-96fb-918dc371bf78">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[My report]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="17" splitType="Stretch">
<staticText>
<reportElement x="0" y="2" width="800" height="15" uuid="5f05f44c-eabb-4aaf-b3c2-6c2512f60f73">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[A page header]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="80" y="0" width="100" height="15" uuid="f4a14a24-b4cb-46d4-a799-f7401178d6b7">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="180" y="0" width="100" height="15" uuid="63b605a9-d94e-449f-b4e8-c5b5fa2352fe">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[Value]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="15" splitType="Stretch">
<componentElement>
<reportElement x="80" y="0" width="200" height="15" uuid="36dcd5f4-e9bd-4577-8ff4-dceacfa42263">
<property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
<property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
<property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
<property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<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="items" uuid="98564efc-611d-4267-b650-20b8ce408049">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("items")]]></dataSourceExpression>
<returnValue fromVariable="sumItem" toVariable="sumItemValueFromTable"/>
</datasetRun>
<jr:column width="100" uuid="594a2c22-50a2-4cb3-bd1b-5b73edb3e8c5">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<jr:detailCell style="Table_TD" height="15">
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textField>
<reportElement x="0" y="0" width="100" height="15" uuid="466d9b29-f34d-4718-8f33-47019eaac2f4">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{itemName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="5a0cc5e4-46b3-463a-8e5e-b14f2444559f">
<jr:detailCell style="Table_TD" height="15">
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textField>
<reportElement x="0" y="0" width="100" height="15" uuid="404014b5-2c4b-4479-8a61-517700f7bdd2"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{itemValue}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
<columnFooter>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="180" height="15" uuid="b27a2374-0c07-429c-820f-d44a52faf5b8">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="9" isBold="true"/>
</textElement>
<text><![CDATA[GENERAL SUM]]></text>
</staticText>
<textField evaluationTime="Band">
<reportElement x="180" y="-1" width="100" height="16" uuid="34659bd7-0bb3-4001-9767-91489dc96329"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{sumItemValueGeneral}]]></textFieldExpression>
</textField>
</band>
</columnFooter>
</jasperReport>
这是我使用的 JSON 数据源:
[
{
"userName": "User A",
"userDescription": "Description A",
"items": [
{
"itemName": "Item A",
"itemValue": 100.0
},
{
"itemName": "Item B",
"itemValue": 200.0
}
]
},
{
"userName": "User B",
"userDescription": "Description B",
"items": [
{
"itemName": "Item A",
"itemValue": 400.0
},
{
"itemName": "Item B",
"itemValue": 300.0
}
]
}
]
按照你的代码布局,一个解决方案是在你的 table 的子数据集中定义另一个类似于 sumItem
变量的变量,因为你只能在 JasperSoft 中为每个变量定义 1 return 值Studio(版本 <=6.6.0 有这个奇怪的错误),如果您直接编辑 jrxml,则可以避免此步骤,而是使用 returnValue.
<variable name="sumItemForTotale" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{itemValue}]]></variableExpression>
</variable>
然后return这个到变量sumItemValueGeneral
计算Sum
<returnValue fromVariable="sumItemForTotale" toVariable="sumItemValueGeneral" calculation="Sum"/>
现在删除变量 sumItemValueGeneral
中的当前表达式并将其设置为 Sum
<variable name="sumItemValueGeneral" class="java.lang.Double" calculation="Sum">
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
是的,您需要同时设置变量和 returnValue 来计算 Sum
我个人觉得有点混乱,只将变量设置为 [= 感觉更合乎逻辑16=],但我猜逻辑是:如果你不在 returnValue 中设置 Sum
(值将被强制复制,因为它直接进入变量)并且如果你不设置 Sum
in variabile,当它得到新的'add'值时,变量将被重新初始化。
完整 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="example_report" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isFloatColumnFooter="true" uuid="98b654fa-866e-4db4-9a3d-372b1e6e06c7">
<style name="Table_TH" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
<style backcolor="#FBFDFF"/>
</conditionalStyle>
</style>
<style name="Table_CH" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
<topPen lineWidth="0.5" lineColor="#000000"/>
<leftPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
<rightPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<subDataset name="items" uuid="940ee07b-57c1-48bd-bbfe-9ee2ac66d304">
<queryString language="json">
<![CDATA[items]]>
</queryString>
<field name="itemName" class="java.lang.String">
<property name="net.sf.jasperreports.json.field.expression" value="itemName"/>
<fieldDescription><![CDATA[itemName]]></fieldDescription>
</field>
<field name="itemValue" class="java.lang.Double">
<property name="net.sf.jasperreports.json.field.expression" value="itemValue"/>
<fieldDescription><![CDATA[itemValue]]></fieldDescription>
</field>
<variable name="sumItem" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{itemValue}]]></variableExpression>
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
<variable name="sumItemForTotale" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{itemValue}]]></variableExpression>
</variable>
</subDataset>
<queryString language="json">
<![CDATA[]]>
</queryString>
<field name="userName" class="java.lang.String">
<fieldDescription><![CDATA[userName]]></fieldDescription>
</field>
<field name="userDescription" class="java.lang.String">
<fieldDescription><![CDATA[userDescription]]></fieldDescription>
</field>
<variable name="sumItemValueGeneral" class="java.lang.Double" calculation="Sum">
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
<variable name="sumItemValueFromTable" class="java.lang.Double" resetType="Group" resetGroup="user">
<initialValueExpression><![CDATA[0.0]]></initialValueExpression>
</variable>
<group name="user">
<groupExpression><![CDATA[$F{userName}]]></groupExpression>
<groupHeader>
<band height="15">
<textField>
<reportElement x="80" y="0" width="720" height="15" uuid="4169f25b-4f54-4aa1-9621-38d9d26fba36"/>
<textElement textAlignment="Left" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{userName} + " - " + $F{userDescription}]]></textFieldExpression>
</textField>
<staticText>
<reportElement x="0" y="0" width="80" height="15" uuid="6764c7d6-fd26-4057-8120-63f4b55f3100"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="9" isBold="true"/>
</textElement>
<text><![CDATA[User:]]></text>
</staticText>
</band>
</groupHeader>
<groupFooter>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="180" height="15" uuid="b61688ee-94d6-4c74-aa6f-035b7bbd6dbd"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="9" isBold="true"/>
</textElement>
<text><![CDATA[USER SUM]]></text>
</staticText>
<textField>
<reportElement x="180" y="0" width="100" height="14" uuid="919abbde-42c3-4503-bcc8-ad68ffac7950"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{sumItemValueFromTable}]]></textFieldExpression>
</textField>
</band>
</groupFooter>
</group>
<title>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="800" height="15" uuid="b71231cd-1292-4cad-96fb-918dc371bf78"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[My report]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="17" splitType="Stretch">
<staticText>
<reportElement x="0" y="2" width="800" height="15" uuid="5f05f44c-eabb-4aaf-b3c2-6c2512f60f73"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[A page header]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="80" y="0" width="100" height="15" uuid="f4a14a24-b4cb-46d4-a799-f7401178d6b7"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
<staticText>
<reportElement x="180" y="0" width="100" height="15" uuid="63b605a9-d94e-449f-b4e8-c5b5fa2352fe"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font size="10" isBold="true"/>
</textElement>
<text><![CDATA[Value]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="15" splitType="Stretch">
<componentElement>
<reportElement x="80" y="0" width="200" height="15" uuid="36dcd5f4-e9bd-4577-8ff4-dceacfa42263"/>
<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="items" uuid="98564efc-611d-4267-b650-20b8ce408049">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("items")]]></dataSourceExpression>
<returnValue fromVariable="sumItem" toVariable="sumItemValueFromTable"/>
<returnValue fromVariable="sumItemForTotale" toVariable="sumItemValueGeneral" calculation="Sum"/>
</datasetRun>
<jr:column width="100" uuid="594a2c22-50a2-4cb3-bd1b-5b73edb3e8c5">
<jr:detailCell style="Table_TD" height="15">
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textField>
<reportElement x="0" y="0" width="100" height="15" uuid="466d9b29-f34d-4718-8f33-47019eaac2f4"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{itemName}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="100" uuid="5a0cc5e4-46b3-463a-8e5e-b14f2444559f">
<jr:detailCell style="Table_TD" height="15">
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textField>
<reportElement x="0" y="0" width="100" height="15" uuid="404014b5-2c4b-4479-8a61-517700f7bdd2"/>
<textElement textAlignment="Right" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{itemValue}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
<columnFooter>
<band height="15" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="180" height="15" uuid="b27a2374-0c07-429c-820f-d44a52faf5b8"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font size="9" isBold="true"/>
</textElement>
<text><![CDATA[GENERAL SUM]]></text>
</staticText>
<textField evaluationTime="Report">
<reportElement x="180" y="-1" width="100" height="16" uuid="34659bd7-0bb3-4001-9767-91489dc96329"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font isBold="true"/>
</textElement>
<textFieldExpression><![CDATA[$V{sumItemValueGeneral}]]></textFieldExpression>
</textField>
</band>
</columnFooter>
</jasperReport>
输出