EasyPoi 动态修改表格标题名称
需求
代码实现
1、实体类
@Data
@Accessors(chain = true)
public class AccountVO implements Serializable {
@Excel(name = "账户ID", orderNum = "2")
private String accountId;
@Excel(name = "账户名称", orderNum = "3")
private String accountName;
@Excel(name = "推广渠道", orderNum = "4")
private String mediaPlatformName;
@Excel(name = "品牌", orderNum = "5")
private String brandNames;
@Excel(name = "资金类型", orderNum = "6")
private String fundTypeName;
}
2、利用反射修改标题
// 获取目标实体类的目标字段
Field field = AccountVO.class.getDeclaredField("brandNames");
// 获取该字段的注解
Excel annotation = field.getAnnotation(Excel.class);
// 获取Excel注解所持有的 InvocationHandler
InvocationHandler handler = Proxy.getInvocationHandler(annotation);
Field hFile = handler.getClass().getDeclaredField("memberValues");
// 设值私有可访问
hFile.setAccessible(true);
Map<Object, Object> map = (Map<Object, Object>) hFile.get(handler);
// 获取配置信息
String barnd = environment.getProperty("config.brandAlias", "品牌");
// 修改属性值
map.put("name", barnd);
到此、即可动态的生成标题名称。