若依框架实现Excel动态下拉(查库)

1.展示界面

2.原理

通过Java反射,获取需要导出的指定类,从类中获取需要数据源的指定字段(即下拉框应该在哪个字段上),

再通过hutool工具包。里面有个方法可以为注解里面的元数据设置指定的值。

2.1 Hutool工具包

java 复制代码
	 	<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-core</artifactId>
            <version>5.8.11</version>
        </dependency>

3.代码实现

在ExcelUtil里面

java 复制代码
 /**
     * 动态设置下拉框的值
     * @param adjustClass 修改指定类(需要在导出Excel类里面的指定类)
     * @param combos 传入Combo的值 (下拉框的值,从数据库中查询)
     * @param fieldName 指定传入类的字段上面值 (需要下拉框的字段)
     */
    public void setFiledCombo(Class<T> adjustClass, String[] combos, String fieldName){
        // 获取类的对象
        Field[] declaredFields = adjustClass.getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            Field declaredField = declaredFields[i];
            declaredField.setAccessible(true);
            if (declaredField.getAnnotation(Excel.class) == null) continue;
            if (declaredField.getName().equals(fieldName)) 
            // 给指定字段赋值
            AnnotationUtil.setValue(declaredField.getAnnotation(Excel.class),"combo",combos);
        }
    }

4.如何使用

java 复制代码
 		// 项目名(库中拿)
        List<String> projectName = Service.getProjectName();
        // 调整项
        List<String> costCollectType = Service2.getCostCollectType();
        ExcelUtil<自己的指定类> util = new ExcelUtil<>(自己的指定类.class);
        // 设置字段调整项目名称
        util.setFiledCombo(自己的指定类.class, projectName.toArray(new String[0]),"下拉框的字段名");
        // 设置字段调整项目 
        util.setFiledCombo(自己的指定类.class, costCollectType.toArray(new String[0]),"下拉框的字段名2");
        // 导出
        util.importTemplateExcel(response,"导出表的名字");

5.总结

通过Java的类反射和Hutool工具包完成了对指定字段上的注解的元数据进行修改。如果有错误,麻烦指出,小弟不才,望跟佬学习。

相关推荐
葡萄城技术团队2 小时前
Excel公式前的“@”符号:是Bug还是黑科技?
科技·bug·excel
Omics Pro21 小时前
华大等NC|微生物多样性与抗菌物质发现
大数据·人工智能·深度学习·语言模型·excel
愚公搬代码1 天前
【愚公系列】《OpenClaw实战指南》012-分析与展示:一句话生成可发给老板的报表与 PPT(Excel/WPS 表格自动化处理)
人工智能·自动化·powerpoint·excel·飞书·wps·openclaw
cnskylee1 天前
【技巧分享】Excel实现聚光灯效果
excel
InfiniSynapse1 天前
打工人ai效率工具:一键修改excel
大数据·人工智能·数据分析·excel·ai编程
默 语1 天前
我用 AtomCode 撸了一个 CSV/Excel 数据可视化面板,真实体验报告
信息可视化·excel·atomgit·atomcode
百事牛科技2 天前
解锁你的文档:Excel 打开密码取消教程
windows·excel
开开心心就好2 天前
体积小巧的图片重复查找工具推荐
linux·运维·服务器·智能手机·自动化·excel·fabric
九转成圣2 天前
Spring Boot 导出 Excel 最佳实践:从 POI 函数式封装到 EasyExcel 的“降维打击”
spring boot·后端·excel