如何让JasperReports子报表拉伸以匹配父报表高度?
JasperReports子报表文本框匹配主列高度问题解决
我刚开始使用JasperReports,制作了包含子报表的两列表格,需要让子报表中的文本框自动拉伸,匹配第一列的高度。
当前效果:
期望效果(通过固定高度实现,现在需要自动匹配):
原代码
主报表
<?xml version="1.0" encoding="UTF-8"?> <!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0 --> <!-- 2023-08-21T21:38:25 --> <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="monitoringBulletinRainSensors" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="dc5d5b62-3121-47f6-bc9c-a0c86a6de260"> <property name="ireport.zoom" value="1.0"/> <property name="ireport.x" value="0"/> <property name="ireport.y" value="0"/> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="test"/> <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false"> <defaultValueExpression><![CDATA["C:\Users\<user>\JaspersoftWorkspace\MyReports\"]]></defaultValueExpression> </parameter> <queryString language="json"> <![CDATA[]]> </queryString> <field name="text" class="java.lang.String"/> <detail> <band height="10" splitType="Prevent"> <property name="local_mesure_unitheight" value="pixel"/> <property name="com.jaspersoft.studio.unit.height" value="px"/> <textField isStretchWithOverflow="true"> <reportElement key="" stretchType="RelativeToBandHeight" x="0" y="0" width="50" height="10" uuid="17091a86-28eb-4f4b-814e-2049fc13b754"> <property name="com.jaspersoft.studio.unit.height" value="px"/> <property name="local_mesure_unitwidth" value="pixel"/> <property name="com.jaspersoft.studio.unit.width" value="px"/> </reportElement> <box> <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> </box> <textElement> <font size="16"/> </textElement> <textFieldExpression><![CDATA[$F{text}]]></textFieldExpression> </textField> <subreport> <reportElement stretchType="RelativeToTallestObject" x="50" y="0" width="100" height="10" isPrintInFirstWholeBand="true" backcolor="#FFFFFF" uuid="d8d61ef2-ee12-437a-be7d-9514dc16438d"> <property name="com.jaspersoft.studio.unit.y" value="pixel"/> <property name="local_mesure_unitheight" value="pixel"/> <property name="com.jaspersoft.studio.unit.height" value="px"/> <property name="local_mesure_unitwidth" value="pixel"/> <property name="com.jaspersoft.studio.unit.width" value="px"/> </reportElement> <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("children")]]></dataSourceExpression> <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "test_child.jasper"]]></subreportExpression> </subreport> </band> </detail> </jasperReport>
子报表
<?xml version="1.0" encoding="UTF-8"?> <!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0 --> <!-- 2023-08-21T21:38:06 --> <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="test_child" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="6ee7b94c-7f06-43f8-a02a-461787c5be45"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/> <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="text" class="java.lang.String"/> <background> <band splitType="Stretch"/> </background> <detail> <band height="30" splitType="Prevent"> <textField isStretchWithOverflow="true"> <reportElement positionType="Float" stretchType="RelativeToBandHeight" x="0" y="0" width="160" height="30" uuid="bcfd27d0-fc93-42bd-82fb-74a8d95ed706"> <property name="local_mesure_unitheight" value="pixel"/> <property name="com.jaspersoft.studio.unit.height" value="px"/> <property name="local_mesure_unity" value="pixel"/> <property name="com.jaspersoft.studio.unit.y" value="px"/> </reportElement> <box> <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> </box> <textElement> <font size="16"/> </textElement> <textFieldExpression><![CDATA[$F{text}]]></textFieldExpression> </textField> </band> </detail> </jasperReport>
JSON数据源
{ "text": "This is a very very very very very very very long text", "children": [ { "text": "This is a short text" }, { "text": "This is a short text" }, { "text": "This is a short text" } ] }
解决方案
核心修改点
主报表调整
- 将detail band的
splitType从Prevent改为Stretch,允许band随内容拉伸 - 子报表元素保留
stretchType="RelativeToTallestObject",确保子报表高度匹配主列的拉伸高度
- 将detail band的
子报表调整
- 修改子报表的
pageWidth和columnWidth为100(与主报表中子报表元素的宽度一致),避免内容溢出 - 将detail band的高度改为10(与主报表detail band初始高度一致),并把
splitType设为Stretch - 文本框的
positionType改为Stretch,width设为100,确保文本框能填充子报表宽度并随band拉伸 - 移除不必要的单位属性,简化配置
- 修改子报表的
修改后的主报表代码
<?xml version="1.0" encoding="UTF-8"?> <!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0 --> <!-- 2023-08-21T21:38:25 --> <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="monitoringBulletinRainSensors" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="dc5d5b62-3121-47f6-bc9c-a0c86a6de260"> <property name="ireport.zoom" value="1.0"/> <property name="ireport.x" value="0"/> <property name="ireport.y" value="0"/> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="test"/> <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false"> <defaultValueExpression><![CDATA["C:\Users\<user>\JaspersoftWorkspace\MyReports\"]]></defaultValueExpression> </parameter> <queryString language="json"> <![CDATA[]]> </queryString> <field name="text" class="java.lang.String"/> <detail> <band height="10" splitType="Stretch"> <property name="local_mesure_unitheight" value="pixel"/> <property name="com.jaspersoft.studio.unit.height" value="px"/> <textField isStretchWithOverflow="true"> <reportElement key="" stretchType="RelativeToBandHeight" x="0" y="0" width="50" height="10" uuid="17091a86-28eb-4f4b-814e-2049fc13b754"> <property name="com.jaspersoft.studio.unit.height" value="px"/> <property name="local_mesure_unitwidth" value="pixel"/> <property name="com.jaspersoft.studio.unit.width" value="px"/> </reportElement> <box> <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> </box> <textElement> <font size="16"/> </textElement> <textFieldExpression><![CDATA[$F{text}]]></textFieldExpression> </textField> <subreport> <reportElement stretchType="RelativeToTallestObject" x="50" y="0" width="100" height="10" isPrintInFirstWholeBand="true" backcolor="#FFFFFF" uuid="d8d61ef2-ee12-437a-be7d-9514dc16438d"> <property name="com.jaspersoft.studio.unit.y" value="pixel"/> <property name="local_mesure_unitheight" value="pixel"/> <property name="com.jaspersoft.studio.unit.height" value="px"/> <property name="local_mesure_unitwidth" value="pixel"/> <property name="com.jaspersoft.studio.unit.width" value="px"/> </reportElement> <dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("children")]]></dataSourceExpression> <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "test_child.jasper"]]></subreportExpression> </subreport> </band> </detail> </jasperReport>
修改后的子报表代码
<?xml version="1.0" encoding="UTF-8"?> <!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0 --> <!-- 2023-08-21T21:38:06 --> <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="test_child" pageWidth="100" pageHeight="842" columnWidth="100" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="6ee7b94c-7f06-43f8-a02a-461787c5be45"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/> <queryString> <![CDATA[]]> </queryString> <field name="text" class="java.lang.String"/> <background> <band splitType="Stretch"/> </background> <detail> <band height="10" splitType="Stretch"> <textField isStretchWithOverflow="true"> <reportElement positionType="Stretch" stretchType="RelativeToBandHeight" x="0" y="0" width="100" height="10" uuid="bcfd27d0-fc93-42bd-82fb-74a8d95ed706"> <property name="com.jaspersoft.studio.unit.height" value="px"/> <property name="com.jaspersoft.studio.unit.width" value="px"/> </reportElement> <box> <topPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <leftPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <bottomPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> <rightPen lineWidth="0.5" lineStyle="Solid" lineColor="#000000"/> </box> <textElement> <font size="16"/> </textElement> <textFieldExpression><![CDATA[$F{text}]]></textFieldExpression> </textField> </band> </detail> </jasperReport>
效果说明
修改后,主报表第一列文本框拉伸时,detail band会随之拉伸,子报表元素会自动匹配band的高度,子报表内部的文本框也会拉伸填充整个子报表高度,最终实现两列高度完全一致的效果。
内容的提问来源于
相关产品推荐
相关产品推荐

