转载:https://blog.csdn.net/shiyun123zw/article/details/79221708
有很多人都说Jasperreports不适合中国式复杂报表,实际上运用好父子报表可以解决大部分问题了。例如下面的表。每个学生的学科数目不固定,且每个学生后有相当于小计的平均分。有点复杂度的报表,可以使用子报表解决。
8.1 设计报表模板
8.1.1 新建主模板DemoReport6_main.jrxml,创建Paramters :title(java.lang.String)、date(java.lang.String)、SUBREPORT_DIR(java.lang.String),创建Fields :stuNo(java.lang.Integer)、
stuName(java.lang.String)、subjectList(java.util.List)
8.1.2 设计模板表头如下,注意宽度:
8.1.3 新建子模板DemoReport6_sub.jrxml,只保留DetailBand(Height设置为20),并设置模板的宽500,高20,且四周Margin都为0
8.1.4 转到DemoReport6_main主模板,将Subreport组件拖入到Detail中,跳出Subreport界面
8.1.5 将stuNo和stuName拖入到Detail中
8.1.6 转到DemoReport6_sub子模板,新建Fields:subjectName(String)、teacherName(String)、score(java.lang.Double).拖入到Detail中,注意宽度和主模板保持一致
8.1.7 新建Variables,命名为averageScore
8.1.8 转回DemoReport6_mian主模块,新建Variables,命名为getAverageScore
拖入到Detail Band中,设置好字体样式
点击 $V{getAverageScore} 显示Properties,设置Appearance中Position Type选中为Fix Relation To Bottom(同样将“平均分”如此设置)
设置TextField的Evaluation Time 选中为Band
8.1.9 将Subreport、$F{stuNo}、$F{stuName}放进一个Element Group里,直接在source中添加(未找到图形化操作)
<detail>
...
<elementGroup>
<subreport>
...
</subreport>
<textField>
... <textFieldExpression><![CDATA[$F{stuNo}]]></textFieldExpression>
</textField>
<textField>
...
<textFieldExpression><![CDATA[$F{stuName}]]></textFieldExpression>
</textField>
</elementGroup>
...
</detail>
保存后:
8.1.10 点击 $F{stuNo} 显示Properties,设置Position Type 为Float,Stretch Type为 ElementGroupHeight,把stuNo和Subreport也如此设置。
8.1.11 点击Subreport 显示Properties,在Subreport中设置Exception为$P{SUBREPORT_DIR}+"DemoReport6_sub.jasper",Data Source Expression为
newnet.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{subjectList})
点击 Edit Return Values
保存主模板,子模板,放到项目中的jrxml中。
8.2 编写Servlet注入数据源
8.2.1 新建 Student类和Subject类
8.2.2 新建JasperServlet6,doGet内容如下:
8.3 启动tomcat,访问servlet,查看html文件
代码如下:
DemoReport6_main.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.0.final using JasperReports Library version 6.4.1 -->
<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="DemoReport6_main" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="0" bottomMargin="0" uuid="4d779d07-059d-4881-9a0c-d6c44c0cdb91">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/>
<property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/>
<parameter name="title" class="java.lang.String">
<parameterDescription><![CDATA[]]></parameterDescription>
</parameter>
<parameter name="date" class="java.lang.String">
<parameterDescription><![CDATA[]]></parameterDescription>
</parameter>
<parameter name="SUBREPORT_DIR" class="java.lang.String"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="stuNo" class="java.lang.Integer"/>
<field name="stuName" class="java.lang.String"/>
<field name="subjectList" class="java.util.List"/>
<variable name="getAverageScore" class="java.lang.Double" incrementType="Report"/>
<title>
<band height="60" splitType="Stretch">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<textField>
<reportElement x="0" y="10" width="800" height="40" uuid="a8f8ea70-0699-4184-8528-a1a49a6e6544">
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑" size="22"/>
</textElement>
<textFieldExpression><![CDATA[$P{title}]]></textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band height="30" splitType="Stretch">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<textField>
<reportElement x="500" y="0" width="300" height="30" uuid="976f9ea4-db44-4dd1-a3a1-e65d1d238a58"/>
<textElement textAlignment="Right" verticalAlignment="Middle">
<font fontName="微软雅黑"/>
</textElement>
<textFieldExpression><![CDATA["日期:"+$P{date}]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<columnHeader>
<band height="30">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<textField>
<reportElement x="0" y="0" width="150" height="30" uuid="18eaa77b-a385-4568-af04-d5300f6153b4">
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
</reportElement>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑" size="16" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["学号"]]></textFieldExpression>
</textField>
<textField>
<reportElement x="150" y="0" width="150" height="30" uuid="edcbb21e-5fc3-4f21-ba73-728918007f8c">
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
<property name="com.jaspersoft.studio.unit.x" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
</reportElement>
<box>
<topPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑" size="16" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["姓名"]]></textFieldExpression>
</textField>
<textField>
<reportElement x="300" y="0" width="200" height="30" uuid="5ddbb580-fdbe-4dd8-9ac7-1f3170127467">
<property name="com.jaspersoft.studio.unit.x" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
</reportElement>
<box>
<topPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑" size="16" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["学科"]]></textFieldExpression>
</textField>
<textField>
<reportElement x="700" y="0" width="100" height="30" uuid="02f12719-113d-49ee-912e-26b3aff68f30"/>
<box>
<topPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑" size="16"/>
</textElement>
<textFieldExpression><![CDATA["分数"]]></textFieldExpression>
</textField>
<textField>
<reportElement x="500" y="0" width="200" height="30" uuid="ba3bd18e-3b52-4dd6-b8d5-fb08c89338d9">
<property name="com.jaspersoft.studio.unit.x" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
</reportElement>
<box>
<topPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑" size="16"/>
</textElement>
<textFieldExpression><![CDATA["任课老师"]]></textFieldExpression>
</textField>
</band>
</columnHeader>
<detail>
<band height="40" splitType="Stretch">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<elementGroup>
<subreport>
<reportElement positionType="Float" stretchType="ElementGroupHeight" x="300" y="0" width="500" height="20" uuid="8df8a2e2-cfda-40f4-9343-a873f2cde5cc">
<property name="com.jaspersoft.studio.unit.x" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{subjectList})]]></dataSourceExpression>
<returnValue subreportVariable="averageScore" toVariable="getAverageScore"/>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR}+"DemoReport6_sub.jasper"]]></subreportExpression>
</subreport>
<textField>
<reportElement positionType="Float" stretchType="ElementGroupHeight" x="0" y="0" width="150" height="20" uuid="5629043a-e904-4e34-8a1c-9660dac1bd8a">
<property name="com.jaspersoft.studio.unit.x" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<box>
<topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑"/>
</textElement>
<textFieldExpression><![CDATA[$F{stuNo}]]></textFieldExpression>
</textField>
<textField>
<reportElement positionType="Float" stretchType="ElementGroupHeight" x="150" y="0" width="150" height="20" uuid="72721815-8bea-4fb8-ac90-931449976faf">
<property name="com.jaspersoft.studio.unit.x" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
<property name="com.jaspersoft.studio.unit.width" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑"/>
</textElement>
<textFieldExpression><![CDATA[$F{stuName}]]></textFieldExpression>
</textField>
</elementGroup>
<textField>
<reportElement positionType="FixRelativeToBottom" x="0" y="20" width="700" height="20" uuid="0dfb2d9d-d9cf-4e45-b854-3aa76e841231">
<property name="com.jaspersoft.studio.unit.x" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<box>
<topPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<leftPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<bottomPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
<rightPen lineWidth="1.0" lineStyle="Solid" lineColor="#000000"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑" isBold="true"/>
</textElement>
<textFieldExpression><![CDATA["平均分"]]></textFieldExpression>
</textField>
<textField evaluationTime="Band">
<reportElement positionType="FixRelativeToBottom" x="700" y="20" width="100" height="20" uuid="17c98f34-8b86-4047-8a12-e7d03b5153de">
<property name="com.jaspersoft.studio.unit.x" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
</reportElement>
<box>
<topPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑"/>
</textElement>
<textFieldExpression><![CDATA[$V{getAverageScore}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="30" splitType="Stretch">
<textField>
<reportElement x="680" y="5" width="100" height="20" uuid="8b559f20-e4a7-4b90-b46a-ac1575514392">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<property name="com.jaspersoft.studio.unit.y" value="pixel"/>
</reportElement>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑"/>
</textElement>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
DemoReport6_sub.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.0.final using JasperReports Library version 6.4.1 -->
<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="DemoReport6_sub" pageWidth="500" pageHeight="20" orientation="Landscape" columnWidth="500" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="9761aedd-513e-48ff-8cb6-60f038ffbaab">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<property name="com.jaspersoft.studio.unit." value="pixel"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="subjectName" class="java.lang.String"/>
<field name="teacherName" class="java.lang.String"/>
<field name="score" class="java.lang.Double"/>
<variable name="averageScore" class="java.lang.Double" calculation="Average">
<variableExpression><![CDATA[$F{score}]]></variableExpression>
</variable>
<detail>
<band height="20" splitType="Stretch">
<property name="com.jaspersoft.studio.unit.height" value="pixel"/>
<textField>
<reportElement x="0" y="0" width="200" height="20" uuid="4715047e-672e-4b21-81d5-1b7f433938e7">
<property name="com.jaspersoft.studio.unit.x" value="pixel"/>
</reportElement>
<box>
<topPen lineWidth="1.0"/>
<leftPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑"/>
</textElement>
<textFieldExpression><![CDATA[$F{subjectName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="200" height="20" uuid="7bf63b11-eb47-460a-978a-395b33fb480d"/>
<box>
<topPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑"/>
</textElement>
<textFieldExpression><![CDATA[$F{teacherName}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="400" y="0" width="100" height="20" uuid="c603aad8-9ba2-4994-90c3-86e37cb46f33">
<property name="com.jaspersoft.studio.unit.x" value="pixel"/>
</reportElement>
<box>
<topPen lineWidth="1.0"/>
<bottomPen lineWidth="1.0"/>
<rightPen lineWidth="1.0"/>
</box>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="微软雅黑"/>
</textElement>
<textFieldExpression><![CDATA[$F{score}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Student.java
package com.jasper.bean;
import java.util.ArrayList;
import java.util.List;
public class Student {
private Integer stuNo;
private String stuName;
private List<Subject> subjectList = new ArrayList<Subject>();
public Integer getStuNo() {
return stuNo;
}
public void setStuNo(Integer stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public List<Subject> getSubjectList() {
return subjectList;
}
public void setSubjectList(List<Subject> subjectList) {
this.subjectList = subjectList;
}
}
Subject.java
package com.jasper.bean;
public class Subject {
private String subjectName;
private String teacherName;
private Double score;
public String getSubjectName() {
return subjectName;
}
public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
public Subject(String subjectName, String teacherName, Double score) {
super();
this.subjectName = subjectName;
this.teacherName = teacherName;
this.score = score;
}
public Subject() {
super();
}
}
JasperServlet6.java
package com.jasper.web;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import com.jasper.bean.Student;
import com.jasper.bean.Subject;
@WebServlet("/JasperServlet6")
public class JasperServlet6 extends HttpServlet {
private static final long serialVersionUID = 1L;
public JasperServlet6() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,Object> parameters = new HashMap<String,Object>(16);
//组装list数据源
Subject sub1 = new Subject("语文","张老师",80.0);
Subject sub2 = new Subject("数学","王老师",90.0);
Subject sub3 = new Subject("物理","孙老师",46.0);
Subject sub4 = new Subject("政治","李老师",50.0);
List<Student> list = new ArrayList<Student>();
Student stu1 = new Student();
stu1.setStuNo(101);
stu1.setStuName("小明");
List<Subject> sublist1 = new ArrayList<Subject>();
sublist1.add(sub1);
sublist1.add(sub2);
stu1.setSubjectList(sublist1);
Student stu2 = new Student();
stu2.setStuNo(102);
stu2.setStuName("小雨");
List<Subject> sublist2 = new ArrayList<Subject>();
sublist2.add(sub2);
sublist2.add(sub3);
sublist2.add(sub4);
stu2.setSubjectList(sublist2);
Student stu3 = new Student();
stu3.setStuNo(103);
stu3.setStuName("小东");
List<Subject> sublist3 = new ArrayList<Subject>();
sublist3.add(sub4);
sublist3.add(sub3);
stu3.setSubjectList(sublist3);
list.add(stu1);
list.add(stu2);
list.add(stu3);
String mainjrxmlPath = request.getServletContext().getRealPath("/")+"/jrxml/DemoReport6_main.jrxml";
String subjrxmlPath = request.getServletContext().getRealPath("/")+"/jrxml/DemoReport6_sub.jrxml";
//由jrxml文件编译后生产jasper文件的路径
String mainjasperPath = request.getServletContext().getRealPath("/")+"/jasper/DemoReport6_main.jasper";
String subjasperPath = request.getServletContext().getRealPath("/")+"/jasper/DemoReport6_sub.jasper";
try {
//编译jrxml生产jasper文件
JasperCompileManager.compileReportToFile(mainjrxmlPath, mainjasperPath);
JasperCompileManager.compileReportToFile(subjrxmlPath, subjasperPath);
parameters.put("title", "学生成绩表");
parameters.put("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
String SUBREPORT_DIR = request.getServletContext().getRealPath("/")+"/jasper/";
parameters.put("SUBREPORT_DIR",SUBREPORT_DIR);
JasperPrint jasperPrint = JasperFillManager.fillReport(mainjasperPath, parameters, new JRBeanCollectionDataSource(list));
//生成html文件(位置:D:/student.html)
JasperExportManager.exportReportToHtmlFile(jasperPrint, "D:/student.html");
} catch (JRException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}