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即可