list最多能放多少数据_PDF多数据源导出

场景说明

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

58b9f8b0d16f84d761f3f3c933766b03.png

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

07b6f752e9c33091f6a3622036662961.png

实现思路

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

动手实现

制作模板

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

5572911f07bcf75f1e81441eac50e476.png

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

9b6a3d878063d81afe56c2e51fd9793a.png

第三步:创建Filed和parameter

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

00c6566bdebe08d3df1c8d2de78458ec.png

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

78028e8abb9d7abf69ea18a00fcc67e4.png

第四步:创建子数据集

06cb0d464c59e585d5edb7fd2a1a3ea8.png

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

71e0133b5996c60fa7b890eb8344f02f.png

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

f10bc2bd4abbc531104bef0bbbd9e0d3.png

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

21b3e1e7dd644d670552a8e354d132ae.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://www.toutiao.com/i6852558150504186371/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值