11、进阶之Jxls2的自定义函数

Jxls2
placeholder image
admin 发布于:2018-04-04 15:45:31
阅读:loading

基本介绍

本章节将在导出逻辑中演示使用Jxls2时在模板中使用自定义的函数来处理相关逻辑。自定义函数的功能是我认为这是Jxls2的最出色的亮点了,当然了有些人不知道它能替我们做什么,比如我们可以将后台返回的数据进行格式化,常常是将日期类型、金额类型格式化;将一些格子按其值的大小显示不同的背景及颜色;特殊逻辑的值的解析;单元格的特殊处理;单元格合并等等等等,为什么它可以做这么多失去了,实际上最最核心的还是它的自定义函数的解析,在link章节我已经提到了,它可以让一个普通的单元格转换成PoiCell单元格(就是这个特点我们可以非常简单的进行特殊逻辑的实现),我们使用PoiApi去*作相对应的单元格,实现各种各样的效果。本示例章节主要实现单元格根据文本值显示文本颜色及背景色、格式化金额数字、单元格显示下拉框、自定义逻辑去两个数中的大数、自定义超链接(这个超链接非util中的调用方式)……等。

参考代码

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.HashMap;

import java.util.List;

import java.util.Map;

 

import org.jxls.area.Area;

import org.jxls.builder.AreaBuilder;

import org.jxls.builder.xls.XlsCommentAreaBuilder;

import org.jxls.common.CellRef;

import org.jxls.common.Context;

import org.jxls.expression.JexlExpressionEvaluator;

import org.jxls.transform.Transformer;

import org.jxls.transform.poi.WritableCellValue;

import org.jxls.transform.poi.WritableHyperlink;

import org.jxls.util.TransformerFactory;

 

import cn.chendd.examples.custom.functions.ColorCellValue;

import cn.chendd.examples.custom.functions.DropdownCellValue;

import cn.chendd.examples.vo.User;

 

/**

 * 自定义function

 */

public class SimpleCustomFunctionJxls {

 

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

     

      //模板文件

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

      Context context = new Context();

      //设置参数变量

      context.putVar("x", 10);

      context.putVar("y", 5);

      //设置绑定集合

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

      context.putVar("dataList", dataList);

     

      Map<String , Object> myFunction = new HashMap<String , Object>();

      myFunction.put("my"new SimpleCustomFunctionJxls());

     

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

      Transformer trans = TransformerFactory.createTransformer(is, os);

      JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator) trans.getTransformationConfig().getExpressionEvaluator();

      evaluator.getJexlEngine().setFunctions(myFunction);

      //载入模板、处理导出

      AreaBuilder areaBuilder = new XlsCommentAreaBuilder(trans);

      List<Area> areaList = areaBuilder.build();

      areaList.get(0).applyAt(new CellRef("Hello!A1"), context);

      trans.write();

      //释放资源

      os.flush();

      os.close();

      is.close();

     

   }

  

   //返回大的数

   public Integer max(Integer x , Integer y){

      return x > y ? x : y;

   }

  

   //给金额前面显示个货币符号

   public Object formatMoney(Object a){

      Object result = null;

      if(a != null){

         return "" + a;

      }

      return result;

   }

  

   //超链接

   public WritableCellValue myHyperlink(String address, String title) {

      return new WritableHyperlink(address, title);

   }

  

   //按值显示背景色

   public WritableCellValue showColor(Integer value) {

      return new ColorCellValue(value);

   }

  

   //生成下拉菜单

   public WritableCellValue downlist(String splitItem , String value){

      return new DropdownCellValue(splitItem, value);

   }

  

}

 

代码说明

代码中设置了解析自定义的函数类为本身,相关的public方法即为支持自定义调用的方法,参数均为模板文件进行传递,如上所定义的相关函数分别为:max比大小取大数、格式化金额加¥、超链接、根据值显示文本、根据值生成下拉框,至于其它关联的类后文中专门提供下载。

模板介绍

image.png

模板说明

模板中xy为直接显示的变量,dataList为集合类型使用each循环显示。

运行示例

image.png

运行说明

示例分别显示了不同的自定义函数解析效果。

相关下载

out_simpleCustomFunction.xls

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

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

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

最后更新:2018-04-04 15:45:31

访问次数:0

评论次数:1+

点赞个数:1,[精品文章:1]

 点赞


 发表评论

当前回复:作者

 评论列表


留言区