阿里 EasyExcel 表头国际化

实体类字段使用EasyExcel提供的注解@ExcelProperty,value 值写成占位符形式 ,匹配 i18n 文件里面的编码。

如:

复制代码
/**
 * 仓库名称
 */
@ExcelProperty("{warehouse.record.warehouseName}")
private String warehouseName;

占位符解析器

复制代码
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PlaceholderResolver {

    /**
     * 默认前缀占位符
     */
    public static final String DEFAULT_PLACEHOLDER_PREFIX = "{";

    /**
     * 默认后缀占位符
     */
    public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";

    /**
     * 默认单例占位符解析器,即占位符前缀为"{", 后缀为"}"
     */
    @Getter
    private static final PlaceholderResolver defaultResolver = new PlaceholderResolver();

    /**
     * 占位符前缀
     */
    private String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX;

    /**
     * 占位符后缀
     */
    private String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX;

    /**
     * 根据替换规则来替换指定模板中的占位符值
     *
     * @param content 要解析的字符串
     * @param rule    解析规则回调
     */
    public String resolveByRule(String content, Function<String, String> rule) {
        int start = content.indexOf(this.placeholderPrefix);
        if (start == -1) {
            return content;
        }
        StringBuilder result = new StringBuilder(content);
        while (start != -1) {
            int end = result.indexOf(this.placeholderSuffix, start);
            //获取占位符属性值,如{id}, 即获取id
            String placeholder = result.substring(start + this.placeholderPrefix.length(), end);
            //替换整个占位符内容,即将{id}值替换为替换规则回调中的内容
            String replaceContent = placeholder.trim().isEmpty() ? "" : rule.apply(placeholder);
            result.replace(start, end + this.placeholderSuffix.length(), replaceContent);
            start = result.indexOf(this.placeholderPrefix, start + replaceContent.length());
        }
        return result.toString();
    }
}

自定义拦截器

复制代码
@RequiredArgsConstructor
public class I18nCellWriteHandler implements CellWriteHandler {

    @Resource
    private MessageSource messageSource;

    @Override
    public void beforeCellCreate(CellWriteHandlerContext context) {
        if (!context.getHead()) {
            return;
        }
        final List<String> originHeadNames = context.getHeadData().getHeadNameList();
        if (CollectionUtils.isEmpty(originHeadNames)) {
            return;
        }
        List<String> newHeadNames = originHeadNames.stream().
                map(headName -> PlaceholderResolver.getDefaultResolver()
                        .resolveByRule(headName, this::getMessage)).
                collect(Collectors.toList());
        context.getHeadData().setHeadNameList(newHeadNames);
    }

    public String getMessage(String code) {
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(code, null, locale);
    }
}

使用

相关推荐
电商数据girl几秒前
酒店旅游类数据采集API接口之携程数据获取地方美食品列表 获取地方美餐馆列表 景点评论
java·大数据·开发语言·python·json·旅游
天天打码1 分钟前
python版本管理工具-pyenv轻松切换多个Python版本
开发语言·python
CircleMouse1 分钟前
基于 RedisTemplate 的分页缓存设计
java·开发语言·后端·spring·缓存
ktkiko117 分钟前
顶层架构 - 消息集群推送方案
java·开发语言·架构
楠奕8 分钟前
python中使用neo4j
开发语言·python·neo4j
zybsjn13 分钟前
后端系统做国际化改造,生成多语言包
java·python·c#
南斯拉夫的铁托19 分钟前
labelimg安装及使用指南(yolo)
开发语言·python·yolo
六bring个六43 分钟前
文件系统交互实现
开发语言·c++·qt·交互
Unity官方开发者社区1 小时前
《Cryptical Path》开发诀窍:像玩游戏一样开发一款类Rogue游戏
java·游戏·玩游戏
jingyu飞鸟1 小时前
Centos7系统(最小化安装)安装zabbix7版本详细文章、nginx源代码配置、php源代码、mysql-yum安装
开发语言·php