使用反射来修改注解的属性值---给Excel表添加或删除字段

使用反射来修改注解的属性值---给Excel表添加或删除字段

2023年7月4日发(作者:)

使⽤反射来修改注解的属性值----给Excel表添加或删除字段使⽤反射来给Excel表添加字段1、实体类中字段如下:@Data@Table(name = "T_ZD")public class Site { /** *

主键 */ @Id @KeySql(sql = "select T_ZD_l from dual", order = ) @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 = laredField("routeName"); Excel excel = otation(); InvocationHandler invocationHandler = ocationHandler(excel); Field excelField = ss().getDeclaredField("memberValues"); essible(true); Map memberValues = (Map) (invocationHandler); ("isColumnHidden", true); } catch (Exception e) { tackTrace(); } teExport(fileName,bean,response); }使⽤反射删除Excel表的字段 1、实体类Route@Data@Table(name = "T_XL")public class Route { /** * 主键 */ @Id @KeySql(sql = "select T_XL_l from dual", order = ) @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 addList, List updateList, List errorList) throws Exception { 。。。。。if(null != enterpriseId){ 。。。。 }else{ EasyPoiUtil easyPoiUtil = new EasyPoiUtil<>(); easyPoiUtil.t = route; //显⽰错误信息的列 lumn("errorMsg", false); orMsg("未查询到企业信息"); (route); } }3、⼯具类public class EasyPoiUtil { /** * 需要被反射的对象,使⽤泛型规范传⼊对象 */ 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 (y(columnName)) { throw new NullPointerException("传⼊的属性列名为空"); } if (target == null) { target = true; } //获取⽬标对象的属性值 Field field = ss().getDeclaredField(columnName); //获取注解反射对象 Excel excelAnion = otation(); //获取代理 InvocationHandler invocationHandler = ocationHandler(excelAnion); Field excelField = ss().getDeclaredField("memberValues"); essible(true); Map memberValues = (Map) (invocationHandler); ("isColumnHidden", target); }}

发布者:admin,转转请注明出处:http://www.yc00.com/news/1688418609a135515.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信