zoukankan      html  css  js  c++  java
  • 使用反射来修改注解的属性值----给Excel表添加或删除字段

    使用反射来给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);
        }
    }
  • 相关阅读:
    PHP:第一章——PHP中字符运算符、比较运算符、错误控制运算符
    PHP:第一章——PHP中逻辑运算符的使用方法
    PHP:第一章——PHP中的算术运算符/递增、递减运算符/赋值运算符
    微信小程序通过js动态修改css样式的方法(交流QQ群:604788754)
    微信小程序跨页面获取数据示例
    JavaEE资源
    java 学习路线
    想以编程为职业,现在正在看毕向东的java基础,接下来应该看什么视频,求前辈们指教。
    2017Java学习路线图,内附完整Java自学视频教程+工具经验+面试
    各种 学习路线图专区
  • 原文地址:https://www.cnblogs.com/zwh0910/p/14708384.html
Copyright © 2011-2022 走看看