場景說明
在使用Jasper+jaspersoftStudio導出用戶列表數(shù)據(jù)導出(如下圖)是比較簡單的,就是把用戶列表數(shù)據(jù),一個List集合放到 JRBeanCollectionDataSource中即可。
但是如果有多個List集合需要導出呢,這個應該怎么辦?比如:一個用戶的集合List,還有一個統(tǒng)計報表(也需要一個List集合數(shù)據(jù))
實現(xiàn)思路
需要用到子數(shù)據(jù)集,如果多出幾個List,就創(chuàng)建多少個子數(shù)據(jù)集Dataset
·動手實現(xiàn)
·制作模板
第一步:新建一個Jasper Report模板,選擇 Blank A4 (A4紙大小的模板),然后 Next 命名為userList.jrxml.
第二步:刪除無用的Band,只留 Title 、Colunn Header、Detail、Summary
第三步:創(chuàng)建Filed和parameter
①、創(chuàng)建Filed,這幾個Field用來導出用戶列表的
②、創(chuàng)建parameter,名稱是chartList,指定類型是ArrayList,這個參數(shù)是用來放圖表中所需數(shù)據(jù)的
第四步:創(chuàng)建子數(shù)據(jù)集
第五步:在模板上拖拽用戶列表數(shù)據(jù),注意指定中文名稱
第六步:在模板上拖拽圖表
注意:我這里的是否顯示圖例改成了false,不然導出會失敗
代碼導出
準備兩個實體類:
用來導出用戶列表
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; //數(shù)量
public PeopleCount(String provinceName, Integer count) {
this.provinceName = provinceName;
this.count = count;
}
}
代碼實現(xiàn)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、準備數(shù)據(jù)
// 2.1 列表數(shù)據(jù)
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(getListData());
// 2.2圖表數(shù)據(jù)
Map params = new HashMap();
params.put("chartList",getChartListData());
JasperPrint jasperPrint = JasperFillManager.fillReport(new FileInputStream(templateFile), params,dataSource);
JasperExportManager.exportReportToPdfStream(jasperPrint,new FileOutputStream("d://用戶列表數(shù)據(jù).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;
}
}
效果如下:
猜你喜歡
什么是枚舉?如何使用枚舉?