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);
    }
相关推荐
南境十里·墨染春水14 分钟前
C++传记(面向对象)虚析构函数 纯虚函数 抽象类 final、override关键字
开发语言·c++·笔记·算法
无巧不成书021819 分钟前
30分钟入门Java:从历史到Hello World的小白指南
java·开发语言
2301_7971727523 分钟前
基于C++的游戏引擎开发
开发语言·c++·算法
比昨天多敲两行1 小时前
C++ 二叉搜索树
开发语言·c++·算法
zs宝来了2 小时前
Playwright 自动发布 CSDN 的完整实践
java
Birdy_x2 小时前
接口自动化项目实战(1):requests请求封装
开发语言·前端·python
海海不瞌睡(捏捏王子)2 小时前
C++ 知识点概要
开发语言·c++
桌面运维家3 小时前
VLAN配置进阶:抑制广播风暴,提升网络效率
开发语言·网络·php
吴声子夜歌3 小时前
TypeScript——基础类型(三)
java·linux·typescript
一轮弯弯的明月3 小时前
Python基础-速通秘籍(下)
开发语言·笔记·python·学习