配置文件pom.xml引入
<dependency>
<groupId>io.github.magic-core</groupId>
<artifactId>excel-boot</artifactId>
<version>2.0</version>
</dependency>
最先定义的参数
/***
* 姓名
*/
@ExportField(columnName = "姓名")
private String name;
/***
* 电话
*/
@ExportField(columnName = "电话")
private String mobile;
controller中返回值是void
@ApiOperation("导出")
@PostMapping(value = "/exportExcel", produces = "application/json")
public void exportExcel(@RequestParam String ids,HttpServletResponse httpServletResponse) {
registerService.exportExcel(ids,httpServletResponse);
}
//service直接按照官方自己改改就行了很顺利
@Override
public void exportExcel(String ids,HttpServletResponse httpServletResponse){
try {
ExcelBoot.ExportBuilder(httpServletResponse, "Register", RegisterExcel.class).
exportResponse(null, new ExportFunction<RegisterExcel, RegisterExcel>() {
@Override
public List<RegisterExcel> pageQuery(RegisterExcel registerExcel, int pageNum, int pageSize) {
String[] split = ids.split(",");
ArrayList<Integer> list = new ArrayList<>();
for (String sp : split) {
int i = Integer.parseInt(sp);
list.add(i);
}
System.out.println("-----------"+list);
List<RegisterExcel> registerExcelList = registerMapper.findRegisterListByIds(list);
System.out.println("+++++++"+registerExcelList);
return registerExcelList;
}
@Override
public RegisterExcel convert(RegisterExcel registerExcel) {
return registerExcel;
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
然后就完成了,全部很顺利,但是导出的文件一打开就是文件损坏,然后开始debug开始找问题,找了一下午导入代码、数据、参数的问题,然并卵
最后最后一不小心点想到是不是别的问题,然后找到了。。。
// Swagger 中的produces真是坑了半天
//最开始 produces = "application/json" 然后改成了下面的请求头
@PostMapping(value = "/exportExcel", produces="application/octet-stream")
嗯,下载完美打开。。。