统计表Excel导出时有五个参数是动态的
如:
名称 | 类型 | x年 | (x+1)年 | (x+2)年 | (x+3)年 | (x+4)年 |
---|---|---|---|---|---|---|
鸡毛掸子 | 生活用品 | 995件 | 996件 | 997件 | 9527件 | 12127件 |
背带裤 | 服装 | 7777件 | 13332件 | 2333件 | 12322件 | 13442件 |
牙膏 | 生活用品 | 885件 | 886件 | 887件 | 1982件 | 12343件 |
导出结果如下:
实体类代码:
import com.ruoyi.common.annotation.Excel;
/**
* @title: DemoExport
**/
public class DemoExport {
@Excel(name = "Before")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
使用反射修改实体类中name的@Excel注解信息:
import com.ruoyi.common.annotation.Excel;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;
/**
* @title: TestExport
**/
public class TestExport {
public static void main(String[] args) {
String str = "After";
try {
//获取DemoExport的name1字段
Field field = DemoExport.class.getDeclaredField("name");
//获取name1字段上的Excel注解实例
Excel ename = field.getAnnotation(Excel.class);
//获取Excel注解实例的 name 属性值
String nameBefore = ename.name();
//打印该值
System.out.println("修改前的值"+nameBefore);
//获取 Excel 这个代理实例所持有的 InvocationHandler
InvocationHandler invocationHandler = Proxy.getInvocationHandler(ename);
// 获取 AnnotationInvocationHandler 的 memberValues 字段
Field hField = invocationHandler.getClass().getDeclaredField("memberValues");
// 因为这个字段事 private final 修饰,所以要打开权限
hField.setAccessible(true);
// 获取 memberValues
Map memberValues = (Map) hField.get(invocationHandler);
// 修改 name 属性值
memberValues.put("name", str);
//获取Excel注解实例的 name 属性值
String nameAfter = ename.name();
//打印该值
System.out.println("修改后的值"+nameAfter);
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}
}
}
执行结果: