导出数据 动态表头 对注解进行代理
1.导出数据的表头 需要根据具体数据来设置
@ExcelProperty("${date1}") 的数据需要用date1的值
/** * date1 */ @ExcelIgnore private String date1; /** * date1_value */ @ExcelProperty("${date1}") private BigDecimal date1Value; /** * date2 */ @ExcelIgnore private String date2; /** * date2_value */ @ExcelProperty("${date2}") private BigDecimal date2Value;
2.工具类
public class MachineBoardProxyUtil { public static final String DATE_N_VALUE = "date[0-9]+Value"; public static <T> void proxy(List<T> list, Class<T> clz, String errorMsg) { if(list == null || list.isEmpty() || clz == null){ return; } T obj = list.get(0); try { //代理导出把21天的标题替换为具体的时间 Field[] declaredFields = clz.getDeclaredFields(); for (int i = 0; i < declaredFields.length; i++) { String name = declaredFields[i].getName(); if (name.matches(DATE_N_VALUE)) { ExcelProperty annotation = declaredFields[i].getAnnotation(ExcelProperty.class); //代理 InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation); Field annotationValues = invocationHandler.getClass().getDeclaredField("memberValues"); annotationValues.setAccessible(true); Map map = (Map) annotationValues.get(invocationHandler); Field declaredField = clz.getDeclaredField(name.replace("Value", "")); declaredField.setAccessible(true); String[] value = (String[]) map.get("value"); value[0] = (String) declaredField.get(obj); map.put("value", value); } } } catch (NoSuchFieldException | IllegalAccessException e) { throw new PtmException(errorMsg); } } }