1.导出类
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.HeadFontStyle;
import lombok.Data;
@Data
@HeadFontStyle(fontHeightInPoints = 14)
@ExcelIgnoreUnannotated
public class bb {
@ExcelProperty("备注1")
private String comment1;
@ExcelProperty("备注2")
private String comment2;
@ExcelProperty("备注3")
private String comment3;
@ExcelProperty("备注4")
private String comment4;
@ExcelProperty("备注5")
private String comment5;
}
2.利用反射,修改ExcelProperty 的value字段
import com.alibaba.excel.annotation.ExcelProperty;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;
public class aa {
public static void main(String[] args) {
getLogExportClass();
}
private static Class getLogExportClass() {
try {
Class srcClass = bb.class;
try {
Field field = null;
field = srcClass.getDeclaredField("comment1");
ExcelProperty excel = field.getAnnotation(ExcelProperty.class);
InvocationHandler excelH = Proxy.getInvocationHandler(excel);
Field excelF = excelH.getClass().getDeclaredField("memberValues");
excelF.setAccessible(true);
Map excelValues = (Map) excelF.get(excelH);
excelValues.put("value", new String[]{"替换后的字段"});
} catch (Exception e) {
e.printStackTrace();
}
return srcClass;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}