jxls中自定义函数的使用

实现逻辑代码:

//获得配置
JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator)transformer.getTransformationConfig().getExpressionEvaluator();
//设置静默模式,不报警告
evaluator.getJexlEngine().setSilent(true);
//函数强制,自定义功能
Map<String, Object> funcs = new HashMap<String, Object>();
funcs.put("utils", new JxlsUtils());    //添加自定义功能
evaluator.getJexlEngine().setFunctions(funcs);
//必须要这个,否者表格函数统计会错乱
jxlsHelper.processTemplate(context, transformer);

这里的utils就是要在excel中使用的函数类名。

JxlsUtils类

public class JxlsUtils {	
    /**
     * 字符串为空转换为特定字符
     * @param target  需要验证的字符串
     * @param fit     匹配的字符串
     * @param change  匹配成功要转换的字符串
     */
    public String fitToChange(String target, String fit, String change) {
        if (fit.equals(target)) {
            return change;
        }
        return target;
    }
}

excel模板中使用:

具体页面操作

发布了56 篇原创文章 · 获赞 14 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/feng_xiaoshi/article/details/103439663