• JasperReport 父子报表


    复杂报表或数据内容较多的时候,可以使用子报表解决。

    1、制作父报表,主要有两种

    1) 父报表中需要显示数据,使用子报表弥补sudio设计的不足。

    2) 父报表不需要显示数据,只是作为子报表的载体,适用于复杂报表的设计。(接下来使用这种)

    2、创建父报表

    命名为: template5_parent.jrxml

    在Basic Elements拖拽Subreport到Detail 1

    选择template4_charts.jrxml,

     点击完成。

    3、父报表传递数据

    在template5_parent上创建Parameters

    名称为sublist, 类型为java.util.List

    4、子报表接收数据

    在Value Class,点击右边的浏览

    查找JRBeanCollectionDataSource

    new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{sublist})

     

     sublist就是父报表定义的Parameters。

    创建Parameters,命名为subpath

    Expression填写$P{subpath}

     然后分表编译模板tempate4_chart.jrxml, template5_parent.jrxml. 

    编译后拷贝到程序中。

    5、Java代码编写

    //父子报表
        @GetMapping("/jasper4_parent_child")
        public void jasper4_parent_child( HttpServletResponse response)
                throws Exception {
            List<StudentCount> studentList = new ArrayList<>();
            for (int i = 1; i <= 6; i++) {
                Random random = new Random();
                int count = ((Double) (random.nextDouble() * 10)).intValue();
                StudentCount s1 = new StudentCount();
                s1.setGrade("grade"+ i );
                s1.setNums(i * 20L + count);
                studentList.add(s1);
            }
    
            HashMap<String, Object> parameters = new HashMap<String, Object>();
            //子报表需要的数据
            parameters.put("sublist",studentList);
            //子报表路径
            Resource subResource = new ClassPathResource("templates/template4_charts.jasper");
            parameters.put("subpath",subResource.getFile().getPath());
            String templatePathParent = "templates/template5_parent.jasper";
            JasperReportUtil.exportToPdf(templatePathParent, parameters, studentList, response);
        }
    

      

    6、效果图

    访问: http://localhost:8080/jasper4_parent_child

    作者:Work Hard Work Smart
    出处:http://www.cnblogs.com/linlf03/
    欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 相关阅读:
    DBA手记(学习)-library cache pin
    DBA手记(学习)-RAC环境下GES TX报警情况处理
    DBA手记(学习)
    flashback query闪回数据
    在CentOS7上安装MySQL5.7-源码包方式
    PL/SQL注册码
    TortoiseGit的安装与配置
    Git安装配置及第一次上传项目到GitHub
    【IDEA使用技巧】(5) —— IntelliJ IDEA集成Tomcat部署Maven Web项目
    【IDEA使用技巧】(4) —— IDEA 构建Java Maven项目、导入Eclipse项目、多Module Maven项目
  • 原文地址:https://www.cnblogs.com/linlf03/p/14883085.html
Copyright © 2020-2023  润新知