以下简单建立一个实体类,增长easypoi的@Excel注解,注解中name的值,便是导出的时候表头的信息,如今以逗号分隔三种语言的文字,根据传入语言标识,从新设置对应的文字。
方法一:
@Data public class StaOrgPost { @Excel(name = "岗位名称,崗位名稱,Post Name", width = 40) private String postName; @Excel(name = "岗位人数,崗位人數,Post Count", width = 10) private Integer userCnt; }
建立一个导出语言工具类,利用反射,从新赋予@Excel中name字段的值code
public class ExcelLangUtils {
private static final int VALUE_SIZE = 3;
private ExcelLangUtils() {
}
public static Class chooseLang(Class<?> pojoClass, String lang)
throws NoSuchFieldException, IllegalAccessException {
//获取实体类中全部字段
Field[] fields = pojoClass.getDeclaredFields();
for (Field field : fields) {
// 获取字段上的注解
Excel anoExcel = field.getAnnotation(Excel.class);
if (anoExcel != null) {
// 获取代理处理器
InvocationHandler invocationHandler = Proxy.getInvocationHandler(anoExcel);
// 获取私有 memberValues 属性
Field f = invocationHandler.getClass().getDeclaredField("memberValues");
f.setAccessible(true);
// 获取实例的属性map
Map<String, Object> memberValues = (Map<String, Object>) f.get(invocationHandler);
// 获取属性值
String excelValue = (String) memberValues.get("name");
if (StringUtils.isNotBlank(excelValue)) {
//根据传入的语言标识,从新设置属性值
List<String> valueList = Arrays.asList(excelValue.split(","));
if (valueList.size() == VALUE_SIZE) {
if (RptConstants.LANGUAGE_ZH.equals(lang)) {
memberValues.put("name", valueList.get(0));
}
if (RptConstants.LANGUAGE_TW.equals(lang)) {
memberValues.put("name", valueList.get(1));
}
if (RptConstants.LANGUAGE_EN.equals(lang)) {
memberValues.put("name", valueList.get(2));
}
}
}
}
}
return pojoClass;
}
}
使用:
workbook = ExcelExportUtil.exportBigExcel(exportParams, ExcelLangUtils.chooseLang(StaOrgPost.class, RptConstants.LANGUAGE_TW), staOrgPostList);
方法二:
@Data
public class StaOrgPost {
@Excel(name = "system.i18n.postName", width = 40)
private String postName;
@Excel(name = "system.i18n.userCnt", width = 10)
private Integer userCnt;
}
建立一个导出语言工具类,利用反射,从新赋予@Excel中name字段的值code
public class ExcelPoiLangUtils {
private ExcelPoiLangUtils() {
}
public static Class chooseLang(Class<?> pojoClass) {
try{
//获取实体类中全部字段
Field[] fields = pojoClass.getDeclaredFields();
for (Field field : fields) {
// 获取字段上的注解
Excel anoExcel = field.getAnnotation(Excel.class);
if (anoExcel != null) {
// 获取代理处理器
InvocationHandler invocationHandler = Proxy.getInvocationHandler(anoExcel);
// 获取私有 memberValues 属性
Field f = invocationHandler.getClass().getDeclaredField("memberValues");
f.setAccessible(true);
// 获取实例的属性map
Map<String, Object> memberValues = (Map<String, Object>) f.get(invocationHandler);
// 获取属性值
String excelValue = (String) memberValues.get("name");
if (StringUtils.isNotBlank(excelValue)) {
//根据配置的语言标识,重新新设置属性值
//MegUtils.getMessage("media.web.video.plan.header.data.1")
String value = MegUtils.getMessage(excelValue);
if(StringUtils.isNotBlank(value)){
memberValues.put("name", value);
}else{
memberValues.put("name", excelValue);
}
}
}
}
}catch (NoSuchFieldException e){
log.error(e.getMessage(), e);
}catch (IllegalAccessException e){
log.error(e.getMessage(), e);
}
return pojoClass;
}
}
使用:
workbook = ExcelExportUtil.exportBigExcel(exportParams, ExcelLangUtils.chooseLang(StaOrgPost.class), staOrgPostList);
读取国际化资源的方法:
/**
* 读取国际化资源
*/
public class MegUtils {
public static MessageSource messageSource = ApplicationContextUtil.getBean(MessageSource.class);
/**
* 根据参数获取国际化资源,委托给Spring的MessageSource
* @param code
* @param args
* @return
*/
public static final String getMessage(String code,Object ...args){
return messageSource.getMessage(code,args, LocaleContextHolder.getLocale());
}
}
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
MediaApplicationContextUtil.applicationContext = applicationContext;
}
public static <T> T getBean(Class<T> requiredType){
return applicationContext.getBean(requiredType);
}
}