場(chǎng)景說(shuō)明
在使用Jasper+jaspersoftStudio導(dǎo)出用戶列表數(shù)據(jù)導(dǎo)出(如下圖)是比較簡(jiǎn)單的,就是把用戶列表數(shù)據(jù),一個(gè)List集合放到 JRBeanCollectionDataSource中即可。
但是如果有多個(gè)List集合需要導(dǎo)出呢,這個(gè)應(yīng)該怎么辦?比如:一個(gè)用戶的集合List,還有一個(gè)統(tǒng)計(jì)報(bào)表(也需要一個(gè)List集合數(shù)據(jù))
實(shí)現(xiàn)思路
需要用到子數(shù)據(jù)集,如果多出幾個(gè)List,就創(chuàng)建多少個(gè)子數(shù)據(jù)集Dataset
·動(dòng)手實(shí)現(xiàn)
·制作模板
第一步:新建一個(gè)Jasper Report模板,選擇 Blank A4 (A4紙大小的模板),然后 Next 命名為userList.jrxml.
第二步:刪除無(wú)用的Band,只留 Title 、Colunn Header、Detail、Summary
第三步:創(chuàng)建Filed和parameter
①、創(chuàng)建Filed,這幾個(gè)Field用來(lái)導(dǎo)出用戶列表的
②、創(chuàng)建parameter,名稱是chartList,指定類型是ArrayList,這個(gè)參數(shù)是用來(lái)放圖表中所需數(shù)據(jù)的
第四步:創(chuàng)建子數(shù)據(jù)集
第五步:在模板上拖拽用戶列表數(shù)據(jù),注意指定中文名稱
第六步:在模板上拖拽圖表
注意:我這里的是否顯示圖例改成了false,不然導(dǎo)出會(huì)失敗
代碼導(dǎo)出
準(zhǔn)備兩個(gè)實(shí)體類:
用來(lái)導(dǎo)出用戶列表
package com.itheima.pojo;
import lombok.Data;
/**
* 員工
*/
@Data
public class People {
private Long id;
private String userName; //員工名
private String phone; //手機(jī)號(hào)
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;
}
}
用來(lái)導(dǎo)出圖表
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;
}
}
代碼實(shí)現(xiàn)PDF導(dǎo)出
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、準(zhǔn)備數(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;
}
}
效果如下:
猜你喜歡
什么是枚舉?沒有枚舉之前,怎么做的?