请叫我小C的gravatar头像
请叫我小C 2023-09-14 13:56:13
deepoove-poi开启springel表达式中文标签无法识别解决方案

在使用deepoove-poi动态渲染的时候是否遇到这样的问题?如果有,本篇文章教你怎么解决。本文章以时间转换讲解。

动态渲染的表达式为:{{注册让日期}},{{密码更改日期}},{{入团时间}},{{入档时间}}

deepoove-poi开启springel表达式中文标签无法识别解决方案

  1. 首先我们查看deepoove-poi的帮助文档,里面描述可以自定义插件,我们实现接口RenderPolicy实现render方法,代码如下
    public class DateTimeRenderPolicy implements RenderPolicy {
        @Override
        public void render(ElementTemplate elementTemplate, Object o, XWPFTemplate xwpfTemplate) {
            XWPFRun run = ((RunTemplate)elementTemplate).getRun();
            Character sign = elementTemplate.getSign();
            String format = DateTypeEnum.toType(sign).getDesc();
            String value = DateUtil.format(DateUtil.parse(o.toString()), format);
            run.setText(value, 0);
        }
    }
  2. 定义时间枚举
  3. @Getter
    @AllArgsConstructor
    public enum DateTypeEnum {
        F('F', "yyyy-MM-dd HH:mm:ss"),
        Y('Y', "yyyy-MM-dd"),
        M('M', "yyyy-MM"),
        C('C', "yyyy年MM月dd日"),
        D('D', "yyyy年MM月"),
        P('P', "M月dd日"),
        T('T', "yyyy年");
    
        private char value;
        private String desc;
    
        public static DateTypeEnum toType(char value) {
            return Stream.of(DateTypeEnum.values()).filter(p -> p.value == value).findAny().orElse(null);
        }
    }
  4. 如何使用?
  • 在初始化的时候注入标签,指定我们刚刚新建的处理器
  • 
    
            Configure config = Configure.builder()
                .addPlugin(DateTypeEnum.F.getValue(), new DateTimeRenderPolicy())
                .addPlugin(DateTypeEnum.Y.getValue(), new DateTimeRenderPolicy())
                .addPlugin(DateTypeEnum.M.getValue(), new DateTimeRenderPolicy())
                .addPlugin(DateTypeEnum.C.getValue(), new DateTimeRenderPolicy())
                .addPlugin(DateTypeEnum.D.getValue(), new DateTimeRenderPolicy())
                .addPlugin(DateTypeEnum.P.getValue(), new DateTimeRenderPolicy())
                .addPlugin(DateTypeEnum.T.getValue(), new DateTimeRenderPolicy())
                .build();
    XWPFTemplate template = XWPFTemplate.compile(res.bodyStream(), config);
    最终使用标签即可{{F注册让日期}},{{Y密码更改日期}},{{M入团时间}},{{C入档时间}}
  • 效果图1模板deepoove-poi开启springel表达式中文标签无法识别解决方案
  • 效果图2渲染后deepoove-poi开启springel表达式中文标签无法识别解决方案

 


打赏

已有1人打赏

最代码官方的gravatar头像
最近浏览
最代码_码鑫源  LV6 3月29日
fellowfun  LV12 3月12日
笨小孩一号  LV22 1月23日
fengzf  LV16 2023年12月7日
lbszxc  LV2 2023年11月30日
叶儿飞  LV4 2023年11月29日
youwuzuichen  LV10 2023年10月9日
请叫我小C  LV19 2023年10月6日
最代码官方  LV167 2023年9月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友