Java poi通过反射动态修改@Excel等注解属性

Java poi通过反射动态修改@Excel等注解属性

@Entity
@Table(name = "test")
@Data
public class Test {
	private static final long serialVersionUID = 1L;

	@Excel(name = "test")
	private String testColumn;
}

    /**
     * 通过反射动态设置导出的Excel列名
     *
     * @param annotatedColumnName:实体类中被@Excel注解的字段名
     * @param annotationFieldName:实体类中被@Excel中注解的属性名
     * @param newAnnotationFieldValue:属性的新值
     */
    private void setExcelAnnotationValue(String annotatedColumnName, String annotationFieldName, String newAnnotationFieldValue){
        try{
            Class<TTechnologyPointInfoEntity> tTechnologyPointInfoEntity = TTechnologyPointInfoEntity.class;
            Field tjTotalPointField =  tTechnologyPointInfoEntity.getDeclaredField(annotatedColumnName);
            Excel excel = tjTotalPointField.getAnnotation(Excel.class);
            InvocationHandler excelInvocationHandler = Proxy.getInvocationHandler(excel);
            Field excelInvocationHandlerField = excelInvocationHandler.getClass().getDeclaredField("memberValues");
            excelInvocationHandlerField.setAccessible(true);
            Map tjTotalPointExcelFieldMap = (Map) excelInvocationHandlerField.get(excelInvocationHandler);
            tjTotalPointExcelFieldMap.put(annotationFieldName, newAnnotationFieldValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(Java,java,poi,excel)