Spring boot hutool poi 添加列的下拉选择列表

2023-12-25 18:47:43
@PostMapping("/downloadOwnerAndHouseTemplate")
    public void downloadOwnerAndHouseTemplate(HttpServletResponse response) throws Exception {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String communityId = loginUser.getSysUser().getCommunityId();

        if(StringUtils.isBlank(communityId)){
            throw new ServiceException("用户未绑定小区,请重新登录重试");
        }

        List<String> allUnitName=new ArrayList<>();
        List<BBuildingUnit> list = bBuildingUnitService.lambdaQuery().eq(BBuildingUnit::getCommunityId, communityId)
                .eq(BBuildingUnit::getPid, 0).list();
        for(BBuildingUnit unit:list){
            List<BBuildingUnit> list1 = bBuildingUnitService.lambdaQuery()
                    .eq(BBuildingUnit::getPid, unit.getId()).list();
            List<String> collect = list1.stream().map(u -> u.getName()).collect(Collectors.toList());
            allUnitName.addAll(collect);
        }

        String[] strings = allUnitName.stream().toArray(String[]::new);

        //导出
        ClassPathResource resource = new ClassPathResource("excelTemplate/ownerAndHouseTemplate.xlsx");
        ExcelReader reader = cn.hutool.poi.excel.ExcelUtil.getReader(resource.getStream());
        Sheet rows = reader.getSheets().get(0);

        //this.setDropDownBox((XSSFSheet) rows,new String[]{"参数1", "参数2", "参数3"},2,99999999,1,7);

        BigExcelWriter bigExcelWriter = new BigExcelWriter(rows);

        //添加下拉框,其中9999不能太大,太大也会导致下拉框出不来
        CellRangeAddressList addressList = new CellRangeAddressList(1, 9999, 4, 4);
        bigExcelWriter.addSelect(addressList,strings);

        // 一次性写出内容,使用默认样式,强制输出标题
        //bigExcelWriter.write(list2);
        //response为HttpServletResponse对象
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
        response.setHeader("Content-Disposition","attachment;filename=file.xlsx");
        ServletOutputStream out=response.getOutputStream();
        // 终止后删除临时文件
        //file.deleteOnExit();
        bigExcelWriter.flush(out, true);
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

文章来源:https://blog.csdn.net/qq_31683775/article/details/135201264
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。