如何在多个列中拆分详细信息,同时以编程方式附加' continuation'后续部分的消息?

时间:2017-06-22 15:34:04

标签: jasper-reports subreport

如果动态大小不适合当前列,我有客户要求在多列中拆分个人信息(名称,地址,辅助地址等)的详细信息。需要注意的是,名称字段后跟单词'(续)',作为后续列中段的标题。

使用jasper-reports可以实现这种行为吗?

我尝试过切换' inBetween'使用beforeDetailEval()和afterDetailEval()挂钩标记,在列标题区域中检查该标志,以选择性地重新打印名称+'续但',但是列标题始终在afterDetailEval()之后调用scriptlet方法方法(对我的小脑子没有意义),因为' inBetween' flag被重置为false。

(注意:细节带已经使用splitType设置为'立即'拉伸'但是这两种方法都没有按预期运行。整个乐队刚开始重新启动下一栏)

这是否可行?如果可行,是否有针对此方案的创意解决方案的任何建议?

此外,还有一种奇怪的行为,显示如果细节分布在多个列中,则细节的第一部分(直到子报表元素)在下一列上重复。这种行为对客户来说也是不受欢迎的,需要进行调整,以便不重复(标题除外)。

这是客户所需的语法:

Name 1                              Name 2
Address 1                           (Continued)
City 1                              AL – Address 3
State 1                             AL - City 3
Zip 1                               AL - State 3
Additional Locations:               AL - Zip 3
AL-Address 1                        ...
AL-City 1
AL-State 1
AL-Zip 1
AL-Address 2
AL-City 2
AL-State 2
AL-Zip 2

Name 2
Address 2
City 2
State 2
Zip 2
Additional Locations:
AL-Address 1
AL – City 1
AL-State 1
AL – Zip 1
AL – Address 2
AL – City 2
AL – State 2
AL – Zip 2

这是我们实际得到的:

Name1                    Name1
Address1                 Address1
City1                    City1
State1                   State1
Zip1                     Zip1
Additional Locations     Additional Locations
AL-Name1                 AL-City2
AL-Address1              AL-State2
AL-City1                 AL-State2
AL-State1
AL-Zip1
AL-Name2
AL-Address2

这是一个主要的jrxml文件示例:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-06-22T09:48:08 -->
<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" columnCount="2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" scriptletClass="path.to.my.scriptlet" uuid="af53d807-7975-4ff7-bfc5-e438944aa795">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Name" class="java.lang.String"/>
    <field name="Address" class="java.lang.String"/>
    <field name="City" class="java.lang.String"/>
    <field name="State" class="java.lang.String"/>
    <field name="Zip" class="java.lang.String"/>
    <field name="AdditionalLocations" class="java.util.List"/>
    <variable name="IN_MIDDLE_OF_DETAIL" class="java.lang.Boolean" calculation="System">
        <initialValueExpression><![CDATA[Boolean.False]]></initialValueExpression>
    </variable>
    <columnHeader>
        <band height="74" splitType="Stretch">
            <textField isBlankWhenNull="true">
                <reportElement x="-10" y="0" width="100" height="30" uuid="258dce49-fb61-4887-bb30-69e80e96d8f1">
                    <printWhenExpression><![CDATA[$V{IN_MIDDLE_OF_DETAIL}]]></printWhenExpression>
                </reportElement>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="-10" y="40" width="100" height="30" uuid="c18b2fab-900b-4641-aaef-2520510a3510">
                    <printWhenExpression><![CDATA[$V{IN_MIDDLE_OF_DETAIL}]]></printWhenExpression>
                </reportElement>
                <textFieldExpression><![CDATA["(CONTINUED)"]]></textFieldExpression>
            </textField>
        </band>
    </columnHeader>
    <detail>
        <band height="426" splitType="Stretch">
            <textField>
                <reportElement x="0" y="10" width="100" height="30" uuid="853a1b0d-a49e-45c4-8183-83cfd69bf5af"/>
                <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="50" width="100" height="30" uuid="46697c6d-1c2f-422b-9170-6b2f36ce13ba"/>
                <textFieldExpression><![CDATA[$F{Address}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="90" width="100" height="30" uuid="31730cd5-0b17-452e-8a22-d16ea2061605"/>
                <textFieldExpression><![CDATA[$F{City}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="120" width="100" height="30" uuid="b4a4c671-dc63-41b4-b080-9ad5b750bb58"/>
                <textFieldExpression><![CDATA[$F{State}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="160" width="100" height="30" uuid="8f8bec7c-3d4a-466b-8b02-2bb82e61904d"/>
                <textFieldExpression><![CDATA[$F{Zip}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement x="0" y="220" width="100" height="110" uuid="8ef583fd-fa23-47d8-80d0-90de4f6478a0"/>
                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{AdditionalLocations})]]></dataSourceExpression>
                <subreportExpression><![CDATA["Blank_A4_1.jasper"]]></subreportExpression>
            </subreport>
            <staticText>
                <reportElement x="10" y="198" width="100" height="30" uuid="4887e0c1-3da2-4350-9454-4a3e33c0fe71"/>
                <text><![CDATA[Additional Locations]]></text>
            </staticText>
        </band>
    </detail>
</jasperReport>

这是示例子报告jrxml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1  -->
<!-- 2017-06-22T10:39:43 -->
<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_1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9b8a1d93-2ee6-4d29-b9ec-7c56a3917729">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="Address" class="java.lang.String"/>
    <field name="City" class="java.lang.String"/>
    <field name="State" class="java.lang.String"/>
    <field name="Zip" class="java.lang.String"/>
    <detail>
        <band height="168" splitType="Stretch">
            <textField isBlankWhenNull="true">
                <reportElement x="0" y="10" width="100" height="30" uuid="3d74e624-6b6f-40e9-87d2-ddb5022668b2"/>
                <textFieldExpression><![CDATA[$F{Address}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="50" width="100" height="30" uuid="5df31fa3-38ee-44a4-8931-e8eef45fb7a6"/>
                <textFieldExpression><![CDATA[$F{City}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="89" width="100" height="30" uuid="964a282f-54c7-44a3-954d-27d1c70d3d0c"/>
                <textFieldExpression><![CDATA[$F{State}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="130" width="100" height="30" uuid="e1349222-d945-4dec-8454-e2d6e3fb6a2a"/>
                <textFieldExpression><![CDATA[$F{Zip}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

这是切换IN_MIDDLE_OF_DETAIL值的基本scriptlet代码:

@Override
public void beforeDetailEval() throws JRScriptletException
{
    setVariableValue("IN_MIDDLE_OF_DETAIL", Boolean.TRUE)
}


@Override
public void afterDetailEval() throws JRScriptletException
{
    setVariableValue("IN_MIDDLE_OF_DETAIL", Boolean.FALSE)
}

1 个答案:

答案 0 :(得分:0)

删除scriptlet,没有必要这样做,让我们使用子报表有自己的页数的事实,因此当它在主报表中断时会计算页面

我将使用的解决方案是子报表中的pageHeader频段,页码上的printWhenExpression(&gt; 1)。

这意味着您希望在子报表拆分为新“页面”(名称和“继续”)时显示的文本进入子报表的pageHeader,并且应打印在所有子报表“页面”上,除了第一个。

由于name似乎不是子报表数据源的一部分,因此我将其作为参数传递给子报表。

子报告中的代码,以便在页面&gt;时显示标题1

<pageHeader>
    <band height="44">
    <printWhenExpression><![CDATA[$V{PAGE_NUMBER}>1]]></printWhenExpression>
    <textField>
        <reportElement x="0" y="0" width="220" height="20" uuid="8cce4cab-6ccf-4ddb-b1f1-508dc97bfcfe"/>
        <textElement verticalAlignment="Middle">
            <font isBold="true"/>
        </textElement>
        <textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
    </textField>
    <textField>
        <reportElement x="0" y="20" width="220" height="19" uuid="c18b2fab-900b-4641-aaef-2520510a3510"/>
        <textElement verticalAlignment="Middle">
            <font isItalic="true"/>
        </textElement>
        <textFieldExpression><![CDATA["(Continued)"]]></textFieldExpression>
    </textField>
</band>
</pageHeader>

示例输出

Example

我减少了页面高度以减少图像

完整示例

**main report 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="Blank_A4" columnCount="2" pageWidth="595" pageHeight="500" columnWidth="277" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="af53d807-7975-4ff7-bfc5-e438944aa795">
	<queryString>
		<![CDATA[]]>
	</queryString>
	<field name="name" class="java.lang.String"/>
	<field name="address" class="java.lang.String"/>
	<field name="city" class="java.lang.String"/>
	<field name="state" class="java.lang.String"/>
	<field name="zip" class="java.lang.String"/>
	<field name="additionalLocations" class="java.util.List"/>
	<detail>
		<band height="141" splitType="Immediate">
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="20" width="280" height="20" uuid="46697c6d-1c2f-422b-9170-6b2f36ce13ba">
					<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
				</reportElement>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="40" width="280" height="20" uuid="31730cd5-0b17-452e-8a22-d16ea2061605"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="60" width="280" height="20" uuid="b4a4c671-dc63-41b4-b080-9ad5b750bb58"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{state}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="80" width="280" height="20" uuid="8f8bec7c-3d4a-466b-8b02-2bb82e61904d"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{zip}]]></textFieldExpression>
			</textField>
			<subreport>
				<reportElement x="0" y="120" width="280" height="20" isPrintWhenDetailOverflows="true" uuid="8ef583fd-fa23-47d8-80d0-90de4f6478a0"/>
				<subreportParameter name="name">
					<subreportParameterExpression><![CDATA[$F{name}]]></subreportParameterExpression>
				</subreportParameter>
				<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{additionalLocations})]]></dataSourceExpression>
				<subreportExpression><![CDATA["C:/Users/pette/JaspersoftWorkspace/MyReports/SplitSubreport.jasper"]]></subreportExpression>
			</subreport>
			<staticText>
				<reportElement x="0" y="100" width="280" height="20" uuid="4887e0c1-3da2-4350-9454-4a3e33c0fe71"/>
				<textElement verticalAlignment="Middle">
					<font isItalic="false" isUnderline="true"/>
				</textElement>
				<text><![CDATA[Additional Locations]]></text>
			</staticText>
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement x="0" y="0" width="280" height="20" uuid="258dce49-fb61-4887-bb30-69e80e96d8f1"/>
				<textElement verticalAlignment="Middle">
					<font isBold="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
</jasperReport>

**subreport 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="Blank_A4_1" pageWidth="280" pageHeight="200" columnWidth="280" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="9b8a1d93-2ee6-4d29-b9ec-7c56a3917729">
	<parameter name="name" class="java.lang.String"/>
	<queryString>
		<![CDATA[]]>
	</queryString>
	<field name="address" class="java.lang.String"/>
	<field name="city" class="java.lang.String"/>
	<field name="state" class="java.lang.String"/>
	<field name="zip" class="java.lang.String"/>
	<pageHeader>
		<band height="44">
			<printWhenExpression><![CDATA[$V{PAGE_NUMBER}>1]]></printWhenExpression>
			<textField>
				<reportElement x="0" y="20" width="220" height="19" uuid="c18b2fab-900b-4641-aaef-2520510a3510"/>
				<textElement verticalAlignment="Middle">
					<font isItalic="true"/>
				</textElement>
				<textFieldExpression><![CDATA["(Continued)"]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="0" y="0" width="220" height="20" uuid="8cce4cab-6ccf-4ddb-b1f1-508dc97bfcfe"/>
				<textElement verticalAlignment="Middle">
					<font isBold="true"/>
				</textElement>
				<textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
			</textField>
		</band>
	</pageHeader>
	<detail>
		<band height="85" splitType="Stretch">
			<textField isStretchWithOverflow="true" isBlankWhenNull="true">
				<reportElement x="0" y="-1" width="220" height="21" uuid="3d74e624-6b6f-40e9-87d2-ddb5022668b2"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="20" width="220" height="20" uuid="5df31fa3-38ee-44a4-8931-e8eef45fb7a6"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="40" width="220" height="20" uuid="964a282f-54c7-44a3-954d-27d1c70d3d0c"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{state}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement x="0" y="60" width="220" height="20" uuid="e1349222-d945-4dec-8454-e2d6e3fb6a2a"/>
				<textElement verticalAlignment="Middle"/>
				<textFieldExpression><![CDATA[$F{zip}]]></textFieldExpression>
			</textField>
		</band>
	</detail>
</jasperReport>