首页 > 其他分享 >PDF多数据源导出

PDF多数据源导出

时间:2023-01-15 10:07:12浏览次数:42  
标签:peopleList String People 数据源 导出 add PDF new import


场景说明

在使用Jasper+jaspersoftStudio导出用户列表数据导出(如下图)是比较简单的,就是把用户列表数据,一个List集合放到 JRBeanCollectionDataSource中即可。

 

PDF多数据源导出_列表

 

但是如果有多个List集合需要导出呢,这个应该怎么办?比如:一个用户的集合List,还有一个统计报表(也需要一个List集合数据)

 

PDF多数据源导出_python_02

 

实现思路

需要用到子数据集,如果多出几个List,就创建多少个子数据集Dataset

动手实现

制作模板

第一步:新建一个Jasper Report模板,选择 Blank A4 (A4纸大小的模板),然后 Next 命名为userList.jrxml.

 

PDF多数据源导出_python_03

 

第二步:删除无用的Band,只留 Title 、Colunn Header、Detail、Summary

 

PDF多数据源导出_可视化_04

 

第三步:创建Filed和parameter

①、创建Filed,这几个Field用来导出用户列表的

 

PDF多数据源导出_列表_05

 

②、创建parameter,名称是chartList,指定类型是ArrayList,这个参数是用来放图表中所需数据的

 

PDF多数据源导出_大数据_06

 

第四步:创建子数据集

 

PDF多数据源导出_大数据_07

 

第五步:在模板上拖拽用户列表数据,注意指定中文名称

 

PDF多数据源导出_数据可视化_08

 

第六步:在模板上拖拽图表

 

PDF多数据源导出_可视化_09

 

注意:我这里的是否显示图例改成了false,不然导出会失败

 

PDF多数据源导出_可视化_10

 

代码导出

准备两个实体类:

用来导出用户列表

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;
}
}

效果如下:

 

PDF多数据源导出_可视化_11

标签:peopleList,String,People,数据源,导出,add,PDF,new,import
From: https://blog.51cto.com/u_8238263/6008214

相关文章