如何检查该行是否在每一页上排在第一位?
How to check if the row is first on each page?
我有要打印的数据列表。我想知道是否有任何方法可以找到该行是否是 JasperReports 报告中每一页的第一行?
有一个内置变量名$V{PAGE_COUNT}
PAGE_COUNT - Built-in variable containing the number of records that were processed when generating the current page.
该变量从页面上的第一条记录开始,计数到页面末尾,当创建新页面时,它将重置为 1。
这意味着页面上的第一条记录将具有此变量 ==1
,例如,如果您想在第一行添加内容,您可以使用 printWhenExpression
。
<printWhenExpression><![CDATA[$V{PAGE_COUNT}.intValue()==1]]></printWhenExpression>
除了使用 JasperReports 引擎内置变量 PAGE_COUNT
.
之外,还可以通过多种方式解决此任务
使用变量
我们可以用 resetType="Page"
创建变量来计算页面的行数。
<variable name="counterOnPage" class="java.lang.Integer" resetType="Page" calculation="Sum">
<variableExpression><![CDATA[1]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
数据源
简单的 csv 数据源将适用于示例。
First page. Row 1
First page. Row 2
First page. Row 3
First page. Row 4
First page. Row 5
Second page. Row 1
Second page. Row 2
Second page. Row 3
Second page. Row 4
Second page. Row 5
Third page. Row 1
Third page. Row 2
Third page. Row 3
Third page. Row 4
Third page. Row 5
下例中此数据源的数据适配器名称为rows.csv。字段的名称是 name
示例报告模板
报表的高度足以让每页只显示 5 行。
<?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="Using variable" pageWidth="250" pageHeight="75" whenNoDataType="AllSectionsNoDetail" columnWidth="210" leftMargin="20" rightMargin="20" topMargin="0" bottomMargin="0">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="rows.csv"/>
<field name="name" class="java.lang.String"/>
<variable name="counterOnPage" class="java.lang.Integer" resetType="Page" calculation="Sum">
<variableExpression><![CDATA[1]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
<detail>
<band height="15">
<textField>
<reportElement x="0" y="0" width="180" height="15" uuid="1b535a7e-7a8e-4e44-91ff-c0b8415afcf1"/>
<textFieldExpression><![CDATA[($V{counterOnPage} == 1) ? $F{name} + "!" : $F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
对于第一行,我们将添加符号“!”到字符串的末尾。
输出结果
结果(pdf 文件)将是:
使用报表的参数映射
这是一个使用参数映射 ($P{REPORT_PARAMETERS_MAP}
) 的小技巧。
我们可以设置一些"flag"(而不是使用变量)来标记页面的第一行。我们可以把 textField 放在 pageHeader 来设置 value isFirst 在 report's Map, 例如.
<textField>
<reportElement x="0" y="0" width="100" height="1"/>
<textFieldExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.put("isFirst", true)]]></textFieldExpression>
</textField>
-- 我们正在初始化标志的值。
我们应该添加检查 isFirst 值并在首次使用后更改此标志的值。伪造的 textField 将完成这项工作
<textField>
<reportElement x="180" y="0" width="0" height="15"/>
<textFieldExpression><![CDATA[((Boolean) $P{REPORT_PARAMETERS_MAP}.put("isFirst", false)) ? "" : ""]]></textFieldExpression>
</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="Using Map" pageWidth="250" pageHeight="76" whenNoDataType="AllSectionsNoDetail" columnWidth="210" leftMargin="20" rightMargin="20" topMargin="0" bottomMargin="0">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="rows.csv"/>
<field name="name" class="java.lang.String"/>
<pageHeader>
<band height="1">
<textField>
<reportElement x="0" y="0" width="100" height="1"/>
<textFieldExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.put("isFirst", true)]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<detail>
<band height="15">
<textField>
<reportElement x="0" y="0" width="180" height="15">
</reportElement>
<textFieldExpression><![CDATA[($P{REPORT_PARAMETERS_MAP}.get("isFirst") != null && ((Boolean) $P{REPORT_PARAMETERS_MAP}.get("isFirst")) == true) ? "! " + $F{name} + "!" : $F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="180" y="0" width="0" height="15"/>
<textFieldExpression><![CDATA[((Boolean) $P{REPORT_PARAMETERS_MAP}.put("isFirst", false)) ? "" : ""]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
对于第一行,我们将添加符号“!”到字符串的开头和结尾。
输出结果
结果(pdf 文件)将是:
我有要打印的数据列表。我想知道是否有任何方法可以找到该行是否是 JasperReports 报告中每一页的第一行?
有一个内置变量名$V{PAGE_COUNT}
PAGE_COUNT - Built-in variable containing the number of records that were processed when generating the current page.
该变量从页面上的第一条记录开始,计数到页面末尾,当创建新页面时,它将重置为 1。
这意味着页面上的第一条记录将具有此变量 ==1
,例如,如果您想在第一行添加内容,您可以使用 printWhenExpression
。
<printWhenExpression><![CDATA[$V{PAGE_COUNT}.intValue()==1]]></printWhenExpression>
除了使用 JasperReports 引擎内置变量 PAGE_COUNT
.
使用变量
我们可以用 resetType="Page"
创建变量来计算页面的行数。
<variable name="counterOnPage" class="java.lang.Integer" resetType="Page" calculation="Sum">
<variableExpression><![CDATA[1]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
数据源
简单的 csv 数据源将适用于示例。
First page. Row 1
First page. Row 2
First page. Row 3
First page. Row 4
First page. Row 5
Second page. Row 1
Second page. Row 2
Second page. Row 3
Second page. Row 4
Second page. Row 5
Third page. Row 1
Third page. Row 2
Third page. Row 3
Third page. Row 4
Third page. Row 5
下例中此数据源的数据适配器名称为rows.csv。字段的名称是 name
示例报告模板
报表的高度足以让每页只显示 5 行。
<?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="Using variable" pageWidth="250" pageHeight="75" whenNoDataType="AllSectionsNoDetail" columnWidth="210" leftMargin="20" rightMargin="20" topMargin="0" bottomMargin="0">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="rows.csv"/>
<field name="name" class="java.lang.String"/>
<variable name="counterOnPage" class="java.lang.Integer" resetType="Page" calculation="Sum">
<variableExpression><![CDATA[1]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
<detail>
<band height="15">
<textField>
<reportElement x="0" y="0" width="180" height="15" uuid="1b535a7e-7a8e-4e44-91ff-c0b8415afcf1"/>
<textFieldExpression><![CDATA[($V{counterOnPage} == 1) ? $F{name} + "!" : $F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
对于第一行,我们将添加符号“!”到字符串的末尾。
输出结果
结果(pdf 文件)将是:
使用报表的参数映射
这是一个使用参数映射 ($P{REPORT_PARAMETERS_MAP}
) 的小技巧。
我们可以设置一些"flag"(而不是使用变量)来标记页面的第一行。我们可以把 textField 放在 pageHeader 来设置 value isFirst 在 report's Map, 例如.
<textField>
<reportElement x="0" y="0" width="100" height="1"/>
<textFieldExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.put("isFirst", true)]]></textFieldExpression>
</textField>
-- 我们正在初始化标志的值。
我们应该添加检查 isFirst 值并在首次使用后更改此标志的值。伪造的 textField 将完成这项工作
<textField>
<reportElement x="180" y="0" width="0" height="15"/>
<textFieldExpression><![CDATA[((Boolean) $P{REPORT_PARAMETERS_MAP}.put("isFirst", false)) ? "" : ""]]></textFieldExpression>
</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="Using Map" pageWidth="250" pageHeight="76" whenNoDataType="AllSectionsNoDetail" columnWidth="210" leftMargin="20" rightMargin="20" topMargin="0" bottomMargin="0">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="rows.csv"/>
<field name="name" class="java.lang.String"/>
<pageHeader>
<band height="1">
<textField>
<reportElement x="0" y="0" width="100" height="1"/>
<textFieldExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}.put("isFirst", true)]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<detail>
<band height="15">
<textField>
<reportElement x="0" y="0" width="180" height="15">
</reportElement>
<textFieldExpression><![CDATA[($P{REPORT_PARAMETERS_MAP}.get("isFirst") != null && ((Boolean) $P{REPORT_PARAMETERS_MAP}.get("isFirst")) == true) ? "! " + $F{name} + "!" : $F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="180" y="0" width="0" height="15"/>
<textFieldExpression><![CDATA[((Boolean) $P{REPORT_PARAMETERS_MAP}.put("isFirst", false)) ? "" : ""]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
对于第一行,我们将添加符号“!”到字符串的开头和结尾。
输出结果
结果(pdf 文件)将是: