spring将选择table表中指定列转化为excel表,集合类型字段接收问题

该博客介绍了一种在SpringBoot中根据用户指定的列名将数据导出为Excel的方法。通过Swagger接口测试,接收活动ID、活动名称及字段名字符串,然后分割字段名字符串并导出相应列的数据到Excel。当未指定字段时,导出所有数据。
摘要由CSDN通过智能技术生成

springboot选择table表中指定列转化为excel表,接收不了@RequestParam(required = true) Integer actId,@RequestParam(required = true) String actName,List fields

我的做法

1、由于是使用swagger文档进行接口的测试,传的参数均为String,顺着这个逻辑,将接受到的参数变为String,然后在代码中使用分割切分出=成自己想要的数据的集合。

     @GetMapping("/checkedExportToExcel.do")
    @ApiOperation("将指定字段活动报名名单导出到excel表格,fields字段为传入过来的列名的集合")
    @ApiImplicitParams
            ({@ApiImplicitParam(name = "actId", value = "活动id", paramType = "query", dataType = "int", required = true)
                    , @ApiImplicitParam(name = "actName", value = "活动名", paramType = "query", dataType = "String", required = true)
                    , @ApiImplicitParam(name = "fields", value = "字段的拼接,例如:姓名,部门", paramType = "query", dataType = "String", required = false)}
            )
    public void checkedExportToExcel(@RequestParam(required = true) Integer actId,
                                     @RequestParam(required = true) String actName,
                                     String fields,
                                     HttpServletResponse response) {
        try {
            //文件名
            String filename = actName + "活动-报名名单" + ".xlsx";
            //获取报名列表(所有状态,不包括亲属)
            List<SignUp> signUpList = signUpService.getSignUpList(actId, 0);
            OutputStream excelOutputStream = null;
            if (!ObjectUtils.isEmpty(signUpList)) {
                if (!ObjectUtils.isEmpty(fields)) {
                    String[] split = fields.split(",");//以逗号分割
                    ArrayList<String> newFields = new ArrayList<>(Arrays.asList(split));
                    //指定字段导出到excel表中
                    excelOutputStream = ExportExcelUtil.export(signUpList, signUpList.size(), newFields);
                } else {
                    //导出到excel表中
                    excelOutputStream = ExportExcelUtil.exportSignUpToExcel(signUpList, signUpList.size());
                }
                response.setContentType("multipart/form-data");
                filename = URLEncoder.encode(filename, "UTF-8");
                response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
                response.getOutputStream().write(((ByteArrayOutputStream) excelOutputStream).toByteArray());
            } else {
                PrintWriter writer = response.getWriter();
                HashMap<Object, Object> map = new HashMap<>();
                map.put("status", 200);
                map.put("msg", "报名参加活动的人数为0啊");
                writer.write(new ObjectMapper().writeValueAsString(map));
                writer.flush();
                writer.close();
            }

        } catch (Exception ex) {
            log.error(ex.getMessage());
            ex.printStackTrace();
        }
    }

2、将 @GetMapping("/checkedExportToExcel.do")改为 @PostMapping("/checkedExportToExcel.do"),然后将
String fields改写为@RequestBody List fields即可

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值