Use reflection to dynamically modify @Excel annotation properties

Business scenario: When we use poi to export data, we usually determine the column name based on @Excel(name="xxx"). Normally this will not change. But here are a few cases. When we need to change according to certain situations, we need to use reflection.

AirQualityRankingResp.java

@Data
public class AirQualityRankingResp {
    
    
    /** 站点名称 */
    @Excel(name = "站点名称")
    private String site;
    private String siteNumber;
    @Excel(name = "分析时间")
    private String dateTime;
    /** 污染因子 */
    private String pollutionFactor;
    @Excel(name = "AQI")
    private Double value;
    /** 排名 */
    @Excel(name = "排名")
    private Integer ranking;
}

Normally use @Excel to export data as follows

insert image description here

Dynamically modify @Excel properties, code implementation

  /**
     * 通过反射动态设置导出的Excel列名
     *
     * @param annotatedColumnName:实体类中被@Excel注解的字段名
     * @param annotationFieldName:实体类中被@Excel中注解的属性名
     * @param newAnnotationFieldValue:属性的新值
     */
    private void setExcelAnnotationValue(String annotatedColumnName, String annotationFieldName, String newAnnotationFieldValue){
    
    
        try{
    
    
            Class<AirQualityRankingResp> airQualityRankingRespClass = AirQualityRankingResp.class;
            Field classDeclaredField =  airQualityRankingRespClass.getDeclaredField(annotatedColumnName);
            Excel excel = classDeclaredField.getAnnotation(Excel.class);
            InvocationHandler excelInvocationHandler = Proxy.getInvocationHandler(excel);
            Field excelInvocationHandlerField = excelInvocationHandler.getClass().getDeclaredField("memberValues");
            excelInvocationHandlerField.setAccessible(true);
            Map map = (Map) excelInvocationHandlerField.get(excelInvocationHandler);
            map.put(annotationFieldName, newAnnotationFieldValue);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
  • Example of use
   @RequestMapping(value = "/rankingAnalysisExportXls")
    public ModelAndView exportXls(HttpServletRequest request, AirRankingReq airRankingReq) {
    
    
        switch (airRankingReq.getName()){
    
    
            case "2":
                setExcelAnnotationValue("value","name","SO2");
                break;
            case "3":
                setExcelAnnotationValue("value","name","NO2");
                break;
            case "4":
                setExcelAnnotationValue("value","name","PM10");
                break;
            case "5":
                setExcelAnnotationValue("value","name","CO");
                break;
            case "6":
                setExcelAnnotationValue("value","name","O3");
                break;
            case "7":
                setExcelAnnotationValue("value","name","PM2.5");
                break;
        }
        List<AirQualityRankingResp> list = lrMonitorConcentrationService.rankingAnalysis(airRankingReq);
        // Step.3 AutoPoi 导出Excel
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "lr_meteorological_data"); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, AirQualityRankingResp.class);
        //update-begin--Author:liusq  Date:20210126 for:图片导出报错,ImageBasePath未设置--------------------
        ExportParams exportParams = new ExportParams("空气质量数据排名" + "报表", "导出人:" + sysUser.getRealname(), "空气质量排名");
        exportParams.setImageBasePath(upLoadPath);
        //update-end--Author:liusq  Date:20210126 for:图片导出报错,ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, list);
        return mv;
    }

insert image description here

I am here to determine the column name of the generated table based on the name value given by the front end. You can judge according to the actual business scenario. You only need to know how to use it here.

Guess you like

Origin blog.csdn.net/qq_45752401/article/details/125993970