easyExcel导出下拉框

该文章展示了一个使用Java的EasyExcel库来处理Excel模板的示例,特别是创建自定义拦截器`CustomSheetOptionWriteHandler`,用于在指定列填充下拉选项数据。在`download`方法中,通过`getOptionGroupList`生成下拉选项数据,然后利用EasyExcel的写入处理器写入到模板并下载。
摘要由CSDN通过智能技术生成
/**
 * 自定义拦截器.填充下拉选项 key 就是 第几列(从0开始),value 就是这一列 你的下拉数据
 *
 */
@Slf4j
@AllArgsConstructor
public class CustomSheetOptionWriteHandler implements SheetWriteHandler {
    /**
     * 每组下拉选 数据
     */
    private Map<Integer, List<String>> optionGroupMap;

    @Override
    public void afterSheetCreate(SheetWriteHandlerContext context) {
        log.info("第{}个Sheet写入成功。", context.getWriteSheetHolder().getSheetNo());
        //获取下拉数据

        Optional.ofNullable(optionGroupMap).orElse(new HashMap<>()).forEach((columnIndex, options) -> {
            // 区间设置 第一列第一行和第二行的数据。由于第一行是头,所以第一、二行的数据实际上是第二三行
            CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(1, 65535, columnIndex, columnIndex);
            DataValidationHelper helper = context.getWriteSheetHolder().getSheet().getDataValidationHelper();
            DataValidationConstraint constraint = helper.createExplicitListConstraint(options.toArray(new String[0]));
            DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
            context.getWriteSheetHolder().getSheet().addValidationData(dataValidation);
        });
    }
}

先创建这个处理器 去填充下拉数据,再使用

@GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        try {
            ClassPathResource cpr = new ClassPathResource("123.xlsx");

            // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("123", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            
            //mock数据
            Map<Integer, List<String>> data = getOptionGroupList();

            EasyExcel.write(response.getOutputStream())
                    .withTemplate(cpr.getInputStream()).
                    registerWriteHandler(new CustomSheetOptionWriteHandler(data))
                    .sheet(0)
                    .doWrite(new ArrayList<>());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 private Map<Integer, List<String>> getOptionGroupList() {
            Map<Integer, List<String>> data = new HashMap<>();
            for (int i = 0; i < 5; i++) {
                if (i == 2) {
                    continue;
                }
                data.put(i, Arrays.asList("列" + i + "选项a", "列" + i + "选项b", "列" + i + "选项c"));
            }
            return data;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想起来就写写

对比有帮助的话,实力允许的话!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值