场景说明
在使用Jasper+jaspersoftStudio导出用户列表数据导出(如下图)是比较简单的,就是把用户列表数据,一个List集合放到 JRBeanCollectionDataSource中即可。
![58b9f8b0d16f84d761f3f3c933766b03.png](https://i-blog.csdnimg.cn/blog_migrate/63c90d557aa9fb0b743d78fd9debfe6d.png)
但是如果有多个List集合需要导出呢,这个应该怎么办?比如:一个用户的集合List,还有一个统计报表(也需要一个List集合数据)
![07b6f752e9c33091f6a3622036662961.png](https://i-blog.csdnimg.cn/blog_migrate/2e307f0a46255184d3e2d62814f9585c.png)
实现思路
需要用到子数据集,如果多出几个List,就创建多少个子数据集Dataset
动手实现
制作模板
第一步:新建一个Jasper Report模板,选择 Blank A4 (A4纸大小的模板),然后 Next 命名为userList.jrxml.
![5572911f07bcf75f1e81441eac50e476.png](https://i-blog.csdnimg.cn/blog_migrate/129a8e8b3766dda9dce58bdde68ce0d3.png)
第二步:删除无用的Band,只留 Title 、Colunn Header、Detail、Summary
![9b6a3d878063d81afe56c2e51fd9793a.png](https://i-blog.csdnimg.cn/blog_migrate/b8d7d1c32bd2bd3a436e99122051e7a0.png)
第三步:创建Filed和parameter
①、创建Filed,这几个Field用来导出用户列表的
![00c6566bdebe08d3df1c8d2de78458ec.png](https://i-blog.csdnimg.cn/blog_migrate/d4979215728c640fd02f7958d75eccb5.png)
②、创建parameter,名称是chartList,指定类型是ArrayList,这个参数是用来放图表中所需数据的
![78028e8abb9d7abf69ea18a00fcc67e4.png](https://i-blog.csdnimg.cn/blog_migrate/ecde10817dd87b630e3c88919931eca8.png)
第四步:创建子数据集
![06cb0d464c59e585d5edb7fd2a1a3ea8.png](https://i-blog.csdnimg.cn/blog_migrate/89f9ac1b15b43c24c0ce598ec866220d.jpeg)
第五步:在模板上拖拽用户列表数据,注意指定中文名称
![71e0133b5996c60fa7b890eb8344f02f.png](https://i-blog.csdnimg.cn/blog_migrate/7f5e3ec631a395775ff1cb9791366759.png)
第六步:在模板上拖拽图表
![f10bc2bd4abbc531104bef0bbbd9e0d3.png](https://i-blog.csdnimg.cn/blog_migrate/2f39f5c5255e672d8b20f262b6e3b7b9.jpeg)
注意:我这里的是否显示图例改成了false,不然导出会失败
![21b3e1e7dd644d670552a8e354d132ae.png](https://i-blog.csdnimg.cn/blog_migrate/a4de89d8b679c8a68a1401b83f2a1d21.png)
代码导出
准备两个实体类:
用来导出用户列表
package com.itheima.pojo;
import lombok.Data;
/**
* 员工
*/
@Data
public class People {
private Long id;
private String userName; //员工名
private String phone; //手机号
private String province; //省份名
private String hireDateStr; // 入职日期
public People(Long id, String userName, String phone, String province, String hireDateStr) {
this.id = id;
this.userName = userName;
this.phone = phone;
this.province = province;
this.hireDateStr = hireDateStr;
}
}
用来导出图表
package com.itheima.pojo;
import lombok.Data;
@Data
public class PeopleCount {
private String provinceName; //省份名
private Integer count; //数量
public PeopleCount(String provinceName, Integer count) {
this.provinceName = provinceName;
this.count = count;
}
}
代码实现PDF导出
package com.itheima.test;
import com.itheima.pojo.People;
import com.itheima.pojo.PeopleCount;
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 java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;
public class PdfDemo {
public static void main(String[] args) throws Exception{
// 1、获取模板文件
String templateFile = "d://userList.jasper";
// 2、准备数据
// 2.1 列表数据
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(getListData());
// 2.2图表数据
Map params = new HashMap();
params.put("chartList",getChartListData());
JasperPrint jasperPrint = JasperFillManager.fillReport(new FileInputStream(templateFile), params,dataSource);
JasperExportManager.exportReportToPdfStream(jasperPrint,new FileOutputStream("d://用户列表数据.pdf"));
}
public static List<People> getListData(){
List<People> peopleList = new ArrayList<>();
peopleList.add(new People(1L, "大一","13800000001","北京市","2001-01-01"));
peopleList.add(new People(2L, "不二","13800000002","河北省","2002-01-02"));
peopleList.add(new People(3L, "张三","13800000003","河北省","2003-03-03"));
peopleList.add(new People(4L, "李四","13800000004","河北省","2004-02-04"));
peopleList.add(new People(5L, "王五","13800000005","河北省","2005-03-05"));
peopleList.add(new People(6L, "赵六","13800000006","河北省","2006-04-06"));
peopleList.add(new People(7L, "沈七","13800000007","河北省","2007-06-07"));
peopleList.add(new People(8L, "酒八","13800000008","河北省","2008-07-08"));
peopleList.add(new People(9L, "第九","13800000009","山东省","2009-03-09"));
peopleList.add(new People(10L, "石十","13800000010","山东省","2010-07-10"));
peopleList.add(new People(11L, "肖十一","13800000011", "山东省","2011-12-11"));
peopleList.add(new People(12L, "星十二","13800000012", "山东省","2012-05-12"));
peopleList.add(new People(13L, "钗十三","13800000013", "山东省","2013-06-13"));
peopleList.add(new People(14L, "贾十四","13800000014", "山东省","2014-06-14"));
peopleList.add(new People(15L, "甄世武","13800000015", "山东省","2015-06-15"));
return peopleList;
}
public static List<PeopleCount> getChartListData(){
List<PeopleCount> peopleCountList = new ArrayList<>();
peopleCountList.add(new PeopleCount("北京市",100));
peopleCountList.add(new PeopleCount("河北省",200));
peopleCountList.add(new PeopleCount("山东省",220));
peopleCountList.add(new PeopleCount("河南省",230));
return peopleCountList;
}
}
效果如下:
![00ee61c156ea8fd5862b310a65a98ec5.png](https://i-blog.csdnimg.cn/blog_migrate/9c66df014f007b5a3de20cfd50f30c5a.png)
来源:https://www.toutiao.com/i6852558150504186371/