java导出动态下拉框excel模板

1.原始模板

2.导出模板,下拉框为数据库中得到动态数据

java 复制代码
public void downloadTemplate(HttpServletResponse response) throws IOException {
        // 所有部门
        List<String, String> departments = expertManageMapper.selectAllDepartment();
        //所有职位
        List<String, String> posts = expertManageMapper.selectAllPost();
        //所有级别
        List<String, String> levels = expertManageMapper.selectAllLevel();

        // 创建或读取已有的Excel模板
        InputStream templateFileStream = new ClassPathResource("/templates/excel/expert_template.xlsx").getInputStream();
        Workbook workbook = new XSSFWorkbook(templateFileStream);
        Sheet sheet = workbook.getSheetAt(0);
        DataValidationHelper validationHelper = sheet.getDataValidationHelper();
        // 工作表的第二行索引
        int firstRowNum = 1;
        // 工作表的最后一行索引
        int lastRowNum = 10000;
        //添加需要校验的单元格,startCol和endCol根据模板中列进行修改
        // 科室校验
        applyDataValidation(validationHelper, sheet, addressList(firstRowNum, lastRowNum, 3, 3), ArrayUtils.toArray(departments));
        // 职位校验
        applyDataValidation(validationHelper, sheet, addressList(firstRowNum, lastRowNum, 4, 4),ArrayUtils.toArray(posts));
        // 等级校验
        applyDataValidation(validationHelper, sheet, addressList(firstRowNum, lastRowNum, 5, 5), ArrayUtils.toArray(levels));
        // 设置响应头信息
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        String encodedFilename = UriUtils.encode("专家信息录入模板.xlsx", StandardCharsets.UTF_8);
        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + encodedFilename);
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            // 将修改后的Excel内容写入到输出流
            workbook.write(outputStream);
        } finally {
            workbook.close();
        }

    }

  /**
     * 简化创建和应用数据验证的过程
     */
    private void applyDataValidation(DataValidationHelper validationHelper, Sheet sheet, CellRangeAddressList addressList, String[] validationData) {
        if (validationData.length > 0) {
            DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(validationData);
            DataValidation validation = validationHelper.createValidation(constraint, addressList);
            sheet.addValidationData(validation);
        }

    }


    /**
     * 创建CellRangeAddressList
     */
    private CellRangeAddressList addressList(int startRow, int endRow, int startCol, int endCol) {
        return new CellRangeAddressList(startRow, endRow, startCol, endCol);
    }
相关推荐
嗯嗯=15 小时前
python学习篇
开发语言·python·学习
全靠bug跑17 小时前
Spring Cache 实战:核心注解详解与缓存过期时间配置
java·redis·springcache
不会c嘎嘎17 小时前
QT中的常用控件 (二)
开发语言·qt
聆风吟º17 小时前
【数据结构手札】空间复杂度详解:概念 | 习题
java·数据结构·算法
计算机程序设计小李同学17 小时前
基于SpringBoot的个性化穿搭推荐及交流平台
java·spring boot·后端
是一个Bug17 小时前
50道核心JVM面试题
java·开发语言·面试
朱朱没烦恼yeye17 小时前
java基础学习
java·python·学习
她和夏天一样热18 小时前
【观后感】Java线程池实现原理及其在美团业务中的实践
java·开发语言·jvm
lkbhua莱克瓦2418 小时前
进阶-索引3-性能分析
开发语言·数据库·笔记·mysql·索引·性能分析
郑州光合科技余经理18 小时前
技术架构:上门服务APP海外版源码部署
java·大数据·开发语言·前端·架构·uni-app·php