7、进阶之Jxls2的数据分组

Jxls2
placeholder image
admin 发布于:2018-04-04 15:13:32
阅读:loading

基本介绍

本章节将在导出逻辑中演示按照某一属性进行分组显示的场景。本示例涉及的jxls:each的表达式函数中的group属性,需要自行另外掌握。前文中在Each数据循环的示例中我们对each数据循环已经有点点了解了,就是在其基础上使用了一下groupBygroupOrder属性并且将数据循环换成嵌套循环。实际上如果需要实现这一分组显示效果直接从后台构造好对应结构的数据即可,比如后台返回一个Map<String,List<User>>>格式的结构,我们对其循环即可实现同样的效果。

参考代码

package cn.chendd.examples;

 

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

 

import org.jxls.common.Context;

import org.jxls.util.JxlsHelper;

 

import cn.chendd.examples.vo.User;

 

/**

 * 简单的数据分组显示

 */

public class SimpleGroupJxls {

 

   public static void main(String[] args) throws Exception {

      //构造集合数据

      List<User> dataList = new ArrayList<User>();

      dataList.add(new User("zhangchunhua" , "" , 120D));

      dataList.add(new User("zhouyu" , "" , 100D));

      dataList.add(new User("sunce" , "" , 110D));

      dataList.add(new User("machao" , "" , 130D));

      dataList.add(new User("caifuren" , "" , 120D));

      dataList.add(new User("zuoci" , "保密" , 150D));

      //载入模板

      InputStream is = SimpleGroupJxls.class.getClass().getResourceAsStream("/cn/chendd/examples/templates/simpleGroup.xls");

      Context context = new Context();

      context.putVar("dataList", dataList);

      OutputStream os = new FileOutputStream(new File("d:\\test\\out_simpleGroup.xls"));

      //指定Sheet文件解析

      JxlsHelper.getInstance().processTemplate(is, os, context);

      os.flush();

      os.close();

      is.close();

   }

 

}

 

package cn.chendd.examples.vo;

 

public class User {

 

   private String name;

   private String sex;

   private Double payment;

  

   private Integer mergerRows;

  

   public User() {

      super();

   }

   public User(String name, String sex, Double payment) {

  

      super();

      this.name = name;

      this.sex = sex;

      this.payment = payment;

   }

  

   public User(String name, String sex, Double payment , Integer mergerRows) {

     

      super();

      this.name = name;

      this.sex = sex;

      this.payment = payment;

      this.mergerRows = mergerRows;

   }

   //省略gettersetter

  

}

 

代码说明

上述代码中我们可以看出由后台设置了 “dataList”的集合对象数据结构,其中每条明细数据属性都有name、sex、payment属性,mergerRows属性暂时先忽略后文使用的,在此基础的数据上,我们将使用按sex性别就行分组显示数据。

模板介绍

image.png


image.png


image.png

模板说明

上述3个模板图片中第一个图片没什么新颖的东西,只是为给出源数据展示,第二个图片是分组的模板,第三个图片是分组时带统计汇总的模板。其中groupBy属性表示集合数据中将要用来分组的,groupOrder是分组显示的属性中用于排序先后的。

运行示例

image.png


image.png

image.png

运行说明

分组示例包括了两个,分别是按性别sex进行分组,貌似这个排序有问题,nv应该在nan后面才对,不过这不是重点,其中第三个图中每块列表还有包括汇总的的计算和所有分组的总计实现。

相关下载

out_simpleGroup.xls

本示例代码会在后文中提供。

上述许多内容已经过时和过期了,留存本篇文章仅为方便个人查看,原始文章的信息参考:

原始链接:https://www.chendd.cn/information/viewInformation/other/230.a

最后更新:2018-04-04 15:13:32

访问次数:1180

评论次数:0

点赞个数:0

 点赞


 发表评论

当前回复:作者

 评论列表


留言区