使用反射来给Excel表添加字段

1、实体类中字段如下:

@Data
@Table(name
= "T_ZD") public class Site { /** * 主键 */ @Id @KeySql(sql = "select T_ZD_SEQ.nextval from dual", order = ORDER.BEFORE) @Column(name = "ID") private Integer id;
  。。。。
@Transient @Excel(name = "线路名称",width = 30,isColumnHidden = false) private String routeName; }

其中@Excel注解的isColumnHidden属性默认为false,即显示

2、我们可以通过反射来将该isColumnHidden属性值由false改为true

@RequestMapping(value = "templateExport")
    public void templateExport(String fileName, String bean, HttpServletResponse response) {
        try {
            Field routeName = Site.class.getDeclaredField("routeName");
            Excel excel = routeName.getAnnotation(Excel.class);
            InvocationHandler invocationHandler = Proxy.getInvocationHandler(excel);
            Field excelField = invocationHandler.getClass().getDeclaredField("memberValues");
            excelField.setAccessible(true);
            Map memberValues = (Map) excelField.get(invocationHandler);
            memberValues.put("isColumnHidden", true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        siteService.templateExport(fileName,bean,response);
    }

使用反射删除Excel表的字段

 1、实体类Route

@Data
@Table(name = "T_XL")
public class Route {
    /**
     * 主键
     */
    @Id
    @KeySql(sql = "select T_XL_SEQ.nextval from dual", order = ORDER.BEFORE)
    @Column(name = "ID")
    private Integer id;
  
  。。。。
//错误信息 @Transient @Excel(name = "错误信息", width = 50, isColumnHidden = true) private String errorMsg; }

2、我们可以通过反射来将该isColumnHidden属性值由true改为false

@Async
    public void checkDataIsExist(Route route, List<Route> addList, List<Route> updateList, List<Route> errorList) throws Exception {
        。。。。。if(null != enterpriseId){
            。。。。
        }else{
            EasyPoiUtil<Route> easyPoiUtil = new EasyPoiUtil<>();
            easyPoiUtil.t = route;
            //显示错误信息的列
            easyPoiUtil.hideColumn("errorMsg", false);
            route.setErrorMsg("未查询到企业信息");
            errorList.add(route);
        }

    }

3、工具类

public class EasyPoiUtil<T> {

    /**
     * 需要被反射的对象,使用泛型规范传入对象
     */
    public T t;

    /**
     * 动态更改EasyPoi中控制列显示的值
     *
     * @param columnName 需要转换的列属性名称
     * @param target     默认true(不显示)
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public void hideColumn(String columnName, Boolean target) throws Exception {
        if (t == null) {
            throw new ClassNotFoundException("未找到目标类");
        }
        if (StringUtils.isEmpty(columnName)) {
            throw new NullPointerException("传入的属性列名为空");
        }
        if (target == null) {
            target = true;
        }
        //获取目标对象的属性值
        Field field = t.getClass().getDeclaredField(columnName);
        //获取注解反射对象
        Excel excelAnion = field.getAnnotation(Excel.class);
        //获取代理
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(excelAnion);
        Field excelField = invocationHandler.getClass().getDeclaredField("memberValues");
        excelField.setAccessible(true);
        Map memberValues = (Map) excelField.get(invocationHandler);
        memberValues.put("isColumnHidden", target);
    }
}

 

posted on 2021-04-27 12:06  周文豪  阅读(1883)  评论(0编辑  收藏  举报