多行的 Jasper 报告边框底部

Jasper report border bottom for multiple lines

我想为以多行显示其内容的文本字段绘制虚线底部边框。

例如:

Address: 104th Street,
         - - - - - - - - - - -- - - - - - - -- - - - - -
         Beside Market Area
         - - - - - -- - - - - -- - - - - -- - - - -- - - 
         Illinois,617273
         - - - - - -- - - - - -- - - - - -- - - - -- - - 

目前,当我将边框底部设置为文本字段时,它以这种方式显示

Address: 104th Street,
         Beside Market Area
         Illinois,617273
         - - - - - -- - - - - -- - - - - -- - - - -- - - 

请帮我看看如何为多行文本的每行设置边框底部

在下面找到我的 jxml 代码

<?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="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ab424386-3966-4b59-9892-31b3fdb6c498">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Mysql Adapter "/>
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <queryString>
        <![CDATA[Select "104th Street,\n Beside Market Area\n Illinois,617273" as address from dual]]>
    </queryString>
    <field name="address" class="java.lang.String"/>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <detail>
        <band height="125" splitType="Stretch">
            <staticText>
                <reportElement x="10" y="30" width="122" height="30" uuid="b2d1bb4b-38b0-47e9-8da8-5ce6a31c98a8"/>
                <textElement textAlignment="Right" verticalAlignment="Middle">
                    <font size="20"/>
                </textElement>
                <text><![CDATA[Address]]></text>
            </staticText>
            <textField isStretchWithOverflow="true">
                <reportElement x="150" y="30" width="390" height="30" uuid="40d76fd8-76d0-4d37-8bc7-bb4e48749df2"/>
                <box leftPadding="10">
                    <bottomPen lineWidth="1.0" lineStyle="Dashed"/>
                </box>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
</jasperReport>

将您的地址分成不同的字段(即 address-line-1address-line2 等. 而不是 地址 中的所有内容)。然后使用 <printWhenExpression> 有条件地包含该行(如果它不是空白或空)。对于要包含的那些(即满足您的条件),您可以为分隔线包含一个嵌套的 <staticText> 字段。

如果您在查询中设法在单独的字段中分解,当然 @mbmast 解决方案很好,但为了回答您的问题,我将向您展示 "crazy stuff" 您可以在 jasper 报告中做什么。

首先,我定义了一个 java.util.List 变量,split 你在字符串中换行。我没有使用 String[],因为这会破坏报告 IDE

<variable name="addressArray" class="java.util.List">
    <variableExpression><![CDATA[Arrays.asList($F{address}.split("\n"))]]></variableExpression>
</variable>

现在我们可以添加textField并且只得到我们想要显示的位置。为了避免 ArrayIndexOutOfBoundsException 使用 printWhenExpression(为了证明这一点,我在示例中放了 4 textField

示例代码

<?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="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ab424386-3966-4b59-9892-31b3fdb6c498">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Mysql Adapter "/>
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <queryString>
        <![CDATA[Select "104th Street,\n Beside Market Area\n Illinois,617273" as address from dual]]>
    </queryString>
    <field name="address" class="java.lang.String"/>
    <variable name="addressArray" class="java.util.List">
        <variableExpression><![CDATA[Arrays.asList($F{address}.split("\n"))]]></variableExpression>
    </variable>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <detail>
        <band height="154" splitType="Stretch">
            <staticText>
                <reportElement x="10" y="30" width="122" height="30" uuid="b2d1bb4b-38b0-47e9-8da8-5ce6a31c98a8"/>
                <textElement textAlignment="Right" verticalAlignment="Middle">
                    <font size="20"/>
                </textElement>
                <text><![CDATA[Address]]></text>
            </staticText>
            <textField isStretchWithOverflow="true">
                <reportElement x="150" y="30" width="390" height="30" isRemoveLineWhenBlank="true" uuid="40d76fd8-76d0-4d37-8bc7-bb4e48749df2">
                    <printWhenExpression><![CDATA[$V{addressArray}.size()>0]]></printWhenExpression>
                </reportElement>
                <box leftPadding="10">
                    <bottomPen lineWidth="1.0" lineStyle="Dashed"/>
                </box>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$V{addressArray}.get(0)]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="150" y="60" width="390" height="30" isRemoveLineWhenBlank="true" uuid="fcba83d9-5a57-45b4-b015-b23a12043784">
                    <printWhenExpression><![CDATA[$V{addressArray}.size()>1]]></printWhenExpression>
                </reportElement>
                <box leftPadding="10">
                    <bottomPen lineWidth="1.0" lineStyle="Dashed"/>
                </box>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$V{addressArray}.get(1)]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="150" y="90" width="390" height="30" isRemoveLineWhenBlank="true" uuid="d39e756a-7e2b-409d-a423-2a8df5dd7378">
                    <printWhenExpression><![CDATA[$V{addressArray}.size()>2]]></printWhenExpression>
                </reportElement>
                <box leftPadding="10">
                    <bottomPen lineWidth="1.0" lineStyle="Dashed"/>
                </box>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$V{addressArray}.get(2)]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement positionType="Float" x="150" y="120" width="390" height="30" isRemoveLineWhenBlank="true" uuid="12fe3b4f-2b13-4b3a-98ae-adc834f89bc1">
                    <printWhenExpression><![CDATA[$V{addressArray}.size()>3]]></printWhenExpression>
                </reportElement>
                <box leftPadding="10">
                    <bottomPen lineWidth="1.0" lineStyle="Dashed"/>
                </box>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$V{addressArray}.get(3)]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
</jasperReport>

结果