JasperReports 报表组Groups
精华
小牛编辑
172浏览
2023-03-14
1 JasperReports 报表组Groups 介绍
JasperReports 中的报表组有助于以逻辑方式组织报告中的数据。一个报表组代表数据源中一系列连续的记录,它们有一些共同点,例如某个报表字段的值。报告组由 <group> 元素定义。一个报告可以有任意数量的组。声明后,可以在整个报告中提及团体。
报表组具有三个要素 :
-
组表达式: 这表示必须更改以启动新数据组的数据。
-
组的头部分 : 帮助将标签放置在分组数据的开头。
-
组的页脚部分:帮助在分组数据的末尾放置标签。
在报表填充时迭代数据源期间,如果组表达式的值发生变化,则会发生组断裂,并且相应的 <groupFooter> 和 <groupHeader> 部分将插入到生成的文档中。
报表组机制不对数据源提供的数据进行任何排序。仅当数据源中的记录已根据报表中使用的组表达式进行排序时,数据分组才能按预期工作。
2 JasperReports 报表组Groups 属性
<group> 元素包含允许我们控制分组数据的布局方式的属性。属性总结在下表中:
属性 | 描述 |
---|---|
name | 这是必须的属性。它按名称引用报表表达式中的组。它遵循我们为报表参数、字段和报告变量提到的相同命名约定。当您想要引用特定报表组时,它可以用于其他 JRXML 属性。 |
isStartNewColumn | 设置为true 时,每个数据组将从新列开始。默认值为false。 |
isStartNewPage | 设置为true 时,每个数据组将在新页面上开始。默认值为false。 |
isResetPageNumber | 设置为true 时,每次新组开始时都会重置报告页码。默认值为false。 |
isReprintHeaderOnEachPage | 当设置为true 时,组标题将在每一页上重新打印。默认值为false。 |
minHeightToStartNewPage | 定义列底部所需的最小垂直空间量,以便将组标题放置在当前列上。金额以报表单位指定。 |
footerPosition | 呈现页面上组页脚的位置,以及它与跟随它的报表部分相关的行为。它的值可以是:Normal、StackAtBottom、ForceAtBottom和CollateAtBottom。默认值为Normal。 |
keepTogether | 当设置为true 时,防止组在第一次尝试时分裂。 |
3 JasperReports 报表组Groups 示例
让我们将一个组 ( CountryGroup )添加到现有报告模板 (JasperReports 模板设计章节 )。统计每个国家/地区的出现次数,并将计数显示为组页脚。在组头中,每条记录的计数都带有前缀。
项目结构如下:
pom.xml文件
<dependencies>
<!-- https://mvnrepository.com/artifact/net.sf.jasperreports/jasperreports -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.17.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
</dependencies>
DataBean实体类:
package cn.xnip;
public class DataBean {
private String name;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
DataBeanList类:
package cn.xnip;
import java.util.ArrayList;
public class DataBeanList {
public ArrayList<DataBean> getDataBeanList() {
ArrayList<DataBean> dataBeanList = new ArrayList<DataBean>();
dataBeanList.add(produce("Manisha", "India"));
dataBeanList.add(produce("Dennis Ritchie", "USA"));
dataBeanList.add(produce("V.Anand", "India"));
dataBeanList.add(produce("Shrinath", "California"));
return dataBeanList;
}
/**
* This method returns a DataBean object,
* with name and country set in it.
*/
private DataBean produce(String name, String country) {
DataBean dataBean = new DataBean();
dataBean.setName(name);
dataBean.setCountry(country);
return dataBean;
}
}
jasper_report_template.jrxml模板文件:
<?xml version = "1.0"?>
<!DOCTYPE jasperReport PUBLIC
"//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<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 = "jasper_report_template" pageWidth = "595"
pageHeight = "842" columnWidth = "515"
leftMargin = "40" rightMargin = "40" topMargin = "50" bottomMargin = "50">
<parameter name = "ReportTitle" class = "java.lang.String"/>
<parameter name = "Author" class = "java.lang.String"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name = "country" class = "java.lang.String">
<fieldDescription><![CDATA[country]]></fieldDescription>
</field>
<field name = "name" class = "java.lang.String">
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<sortField name = "country" order = "Descending"/>
<sortField name = "name"/>
<variable name = "CountryNumber" class = "java.lang.Integer"
incrementType = "Group" incrementGroup = "CountryGroup"
calculation = "Count">
<variableExpression><![CDATA[Boolean.TRUE]]></variableExpression>
</variable>
<group name = "CountryGroup" minHeightToStartNewPage = "60">
<groupExpression><![CDATA[$F{country}]]></groupExpression>
<groupHeader>
<band height = "20">
<textField evaluationTime = "Group" evaluationGroup = "CountryGroup"
bookmarkLevel = "1">
<reportElement mode = "Opaque" x = "0" y = "5" width = "515"
height = "15" backcolor = "#C0C0C0"/>
<box leftPadding = "10">
<bottomPen lineWidth = "1.0"/>
</box>
<textElement/>
<textFieldExpression class = "java.lang.String">
<![CDATA[" " + String.valueOf($V{CountryNumber}) + ". "
+ String.valueOf($F{country})]]>
</textFieldExpression>
<anchorNameExpression>
<![CDATA[String.valueOf($F{country})]]>
</anchorNameExpression>
</textField>
</band>
</groupHeader>
<groupFooter>
<band height = "20">
<staticText>
<reportElement x = "400" y = "1" width = "60" height = "15"/>
<textElement textAlignment = "Right"/>
<text><![CDATA[Count :]]></text>
</staticText>
<textField>
<reportElement x = "460" y = "1" width = "30" height = "15"/>
<textElement textAlignment = "Right"/>
<textFieldExpression class = "java.lang.Integer">
<![CDATA[$V{CountryGroup_COUNT}]]>
</textFieldExpression>
</textField>
</band>
</groupFooter>
</group>
<title>
<band height = "70">
<line>
<reportElement x = "0" y = "0" width = "515" height = "1"/>
</line>
<textField isBlankWhenNull = "true" bookmarkLevel = "1">
<reportElement x = "0" y = "10" width = "515" height = "30"/>
<textElement textAlignment = "Center">
<font size = "22"/>
</textElement>
<textFieldExpression class = "java.lang.String">
<![CDATA[$P{ReportTitle}]]>
</textFieldExpression>
<anchorNameExpression>
<![CDATA["Title"]]>
</anchorNameExpression>
</textField>
<textField isBlankWhenNull = "true">
<reportElement x = "0" y = "40" width = "515" height = "20"/>
<textElement textAlignment = "Center">
<font size = "10"/>
</textElement>
<textFieldExpression class = "java.lang.String">
<![CDATA[$P{Author}]]>
</textFieldExpression>
</textField>
</band>
</title>
<columnHeader>
<band height = "23">
<staticText>
<reportElement mode = "Opaque" x = "0" y = "3" width = "535" height = "15"
backcolor = "#70A9A9" />
<box>
<bottomPen lineWidth = "1.0" lineColor = "#CCCCCC" />
</box>
<textElement />
<text>
<![CDATA[]]>
</text>
</staticText>
<staticText>
<reportElement x = "414" y = "3" width = "121" height = "15" />
<textElement textAlignment = "Center" verticalAlignment = "Middle">
<font isBold = "true" />
</textElement>
<text><![CDATA[Country]]></text>
</staticText>
<staticText>
<reportElement x = "0" y = "3" width = "136" height = "15" />
<textElement textAlignment = "Center" verticalAlignment = "Middle">
<font isBold = "true" />
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height = "16">
<staticText>
<reportElement mode = "Opaque" x = "0" y = "0" width = "535" height = "14"
backcolor = "#E5ECF9" />
<box>
<bottomPen lineWidth = "0.25" lineColor = "#CCCCCC" />
</box>
<textElement />
<text>
<![CDATA[]]>
</text>
</staticText>
<textField>
<reportElement x = "414" y = "0" width = "121" height = "15" />
<textElement textAlignment = "Center" verticalAlignment = "Middle">
<font size = "9" />
</textElement>
<textFieldExpression class = "java.lang.String">
<![CDATA[$F{country}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x = "0" y = "0" width = "136" height = "15" />
<textElement textAlignment = "Center" verticalAlignment = "Middle" />
<textFieldExpression class = "java.lang.String">
<![CDATA[$F{name}]]>
</textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
先编译报表文件:
package cn.xnip;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.view.JasperViewer;
public class JasperReportFill {
public static void main(String[] args) {
String filePath = "D:/idea_codes/jasper_reports/jasper_report_template.jrxml";
try {
JasperCompileManager.compileReportToFile(filePath,"d:/jasper_report_template.jasper");
System.out.println("编译成功");
} catch (JRException e) {
e.printStackTrace();
}
}
}
编译后在D:/盘根目录下生成jasper_report_template.jasper文件,然后再填充数据:
package cn.xnip;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class JasperReportFill {
public static void main(String[] args) {
String sourceFileName = "d:/jasper_report_template.jasper";
DataBeanList DataBeanList = new DataBeanList();
ArrayList<DataBean> dataList = DataBeanList.getDataBeanList();
JRBeanCollectionDataSource beanColDataSource =
new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
/**
* Passing ReportTitle and Author as parameters
*/
parameters.put("ReportTitle", "List of Contacts");
parameters.put("Author", "Prepared By Manisha");
try {
JasperFillManager.fillReportToFile(
sourceFileName, parameters, beanColDataSource);
} catch (JRException e) {
e.printStackTrace();
}
}
}
填充数据完毕后,在D:/盘根目录下生成jasper_report_template.jrprint文件,最后使用预览程序预览:
package cn.xnip;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.view.JasperViewer;
public class JasperReportFill {
public static void main(String[] args) {
String filePath = "D:/jasper_report_template.jrprint";
try {
JasperViewer.viewReport(filePath,false);
} catch (JRException e) {
e.printStackTrace();
}
}
}
最后输出结果如下:
从效果我们看到,每个国家都被分组,每个国家的出现次数显示在每个组的页脚。