阿里 EasyExcel 表头国际化

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

如:

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

占位符解析器

java 复制代码
@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();
    }
}

自定义拦截器

java 复制代码
@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);
    }
}
相关推荐
YouYOUyouFairy13 分钟前
EXCEL动态表格
excel
苦夏木禾19 分钟前
js请求避免缓存的三种方式
开发语言·javascript·缓存
超级土豆粉27 分钟前
Turndown.js: 优雅地将 HTML 转换为 Markdown
开发语言·javascript·html
Maruko3101 小时前
Java 实现excel大批量导出
excel·poi
忧郁的蛋~1 小时前
HTML表格导出为Excel文件的实现方案
前端·html·excel
wei_shuo1 小时前
飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
java·开发语言·飞算javaai
熊猫钓鱼>_>1 小时前
用Python解锁图像处理之力:从基础到智能应用的深度探索
开发语言·图像处理·python
GO兔2 小时前
开篇:GORM入门——Go语言的ORM王者
开发语言·后端·golang·go
好开心啊没烦恼2 小时前
Python 数据分析:numpy,抽提,整数数组索引与基本索引扩展(元组传参)。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy·pandas
fzyz1232 小时前
Windows系统下WSL从C盘迁移方案
人工智能·windows·深度学习·wsl