【问题标题】:How to insert empty row at the table based on condition in case using JRBeanCollectionDataSource?在使用 JRBeanCollectionDataSource 的情况下,如何根据条件在表中插入空行?
【发布时间】:2019-05-26 22:50:54
【问题描述】:

有一个列表,我使用这个机制将它传递给我的 jasper 报告:

JRBeanCollectionDataSource jrBeanCollectionDataSource = new JRBeanCollectionDataSource(list);

该列表的对象如下:

public class Test{
  private int id;
  //other properties  + getters & setter
}

现在假设我的列表中有 10 个元素(id= 1 到 10),我想用一个空行将它们分隔在一个表中,这意味着顶部 5 行,一个空行,向下 5 行。

怎么做?

【问题讨论】:

  • @AlexK 我应该把分页符放在哪里?在表格单元格上?
  • @AlexK 附加的链接没有帮助...

标签: jasper-reports javabeans


【解决方案1】:

subreport 元素可以帮助解决这个问题 - 我们可以使用 printWhenExpression 根据条件显示或隐藏子报告。

在您的情况下,我们需要用一个空行显示子报表。

示例

显示空行的子报表非常简单。它不需要数据源来显示数据 - 我们可以将 staticText 放在 Title 波段。我们也不需要边距。

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="Empty Line" pageWidth="572" pageHeight="30" whenNoDataType="AllSectionsNoDetail" columnWidth="572" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <style name="bordered">
        <box>
            <pen lineWidth="0.25"/>
        </box>
    </style>
    <title>
        <band height="30" splitType="Stretch">
            <staticText>
                <reportElement style="bordered" x="0" y="0" width="572" height="30"/>
            </staticText>
        </band>
    </title>
</jasperReport>

例如,我使用了简单的 csv 数据源 - 文件 films.csv 包含用于报告的数据:

id,name,year,rating
1,The Shawshank Redemption,1994,9.3
2,The Godfather,1972,9.2
3,The Dark Knight,2008,9.0
4,The Godfather: Part II,1974,9.0
5,The Lord of the Rings: The Return of the King,2003,8.9
6,Pulp Fiction,1994,8.9
7,Schindler's List,1993,8.9
8,"The Good, the Bad and the Ugly",1966,8.9
9,12 Angry Men,1957,8.9
10,Avengers: Endgame,2019,8.8

主要(主)报告正在使用 CSV 文件数据适配器来构建报告。

主报告的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="Insert blank row on condition" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" >
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="films.csv"/>
    <style name="bordered">
        <box>
            <pen lineWidth="0.25"/>
        </box>
    </style>
    <field name="id" class="java.lang.Integer"/>
    <field name="name" class="java.lang.String"/>
    <field name="year" class="java.lang.String"/>
    <field name="rating" class="java.math.BigDecimal"/>
    <title>
        <band height="30" splitType="Stretch">
            <staticText>
                <reportElement x="158" y="0" width="256" height="30"/>
                <text><![CDATA[Films. Showing empty line for Id == 5]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="30">
            <staticText>
                <reportElement x="0" y="0" width="70" height="30"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Id]]></text>
            </staticText>
            <staticText>
                <reportElement x="70" y="0" width="290" height="30"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement x="360" y="0" width="69" height="30"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Year]]></text>
            </staticText>
            <staticText>
                <reportElement x="429" y="0" width="143" height="30"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[Rating]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="60">
            <subreport>
                <reportElement x="0" y="30" width="572" height="30" isRemoveLineWhenBlank="true" >
                    <printWhenExpression><![CDATA[$F{id} == 5]]></printWhenExpression>
                </reportElement>
                <subreportExpression><![CDATA["empty_row.jasper"]]></subreportExpression>
            </subreport>
            <textField>
                <reportElement style="bordered" x="0" y="0" width="70" height="30"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="bordered" x="70" y="0" width="290" height="30"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="bordered" x="360" y="0" width="69" height="30"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression><![CDATA[$F{year}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement style="bordered" x="429" y="0" width="143" height="30"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression><![CDATA[$F{rating}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

我使用&lt;printWhenExpression&gt;&lt;![CDATA[$F{id} == 5]]&gt;&lt;/printWhenExpression&gt; 来隐藏带有空行的子报表,这意味着空行将只显示带有id == 5 的行。你可以使用任何你想要的表达式。

JSS 的输出将是:

替代解决方案

  • 您可以在构建集合期间插入元素,然后将这个集合传递到 JRBeanCollectionDataSource

  • 您可以通过一些逻辑实现基于JRBeanCollectionDataSource的自定义DataSource。

【讨论】:

    猜你喜欢
    • 2017-06-30
    • 2013-04-09
    • 2022-06-13
    • 1970-01-01
    • 2023-02-26
    • 2015-09-27
    • 1970-01-01
    • 2017-07-31
    相关资源
    最近更新 更多