导出
总结了三种导出数据为表格的情况,直接上代码!!
整体导出
后端接口:
// 批量导出数据
@GetMapping("/export")
public void exportDate(HttpServletResponse response) throws IOException {
ExcelWriter writer= ExcelUtil.getWriter(true);
// 全部导出
Iterable<Hero> list=heroService.list();
writer.write(list,true);
// 设置响应格式
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
String filename= URLEncoder.encode("信息表","UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");
ServletOutputStream outputStream= response.getOutputStream();
writer.flush(outputStream,true);
outputStream.flush();
outputStream.close();
}
前端使用:
//批量导出
expertDate () {
window.open('http://localhost:8080/hero/export?token=' + this.user.token)
}
按查询结果导出
后端:
@GetMapping("/export")
public void exportDate(@RequestParam(required = false) String heroname,
@RequestParam(required = false) String herosex,
HttpServletResponse response) throws IOException {
ExcelWriter writer= ExcelUtil.getWriter(true);
// 导出表格
QueryWrapper<Hero> queryWrapper = new QueryWrapper<Hero>().orderByDesc("id");
queryWrapper.like(StrUtil.isNotBlank(heroname), "heroname", heroname);
queryWrapper.like(StrUtil.isNotBlank(herosex), "herosex", herosex);
Iterable<Hero> list=heroService.list(queryWrapper);
writer.write(list,true);
// 设置响应格式
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
String filename= URLEncoder.encode("特工表","UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");
ServletOutputStream outputStream= response.getOutputStream();
writer.flush(outputStream,true);
outputStream.flush();
outputStream.close();
}
前端:
expertDate () {
window.open('http://localhost:8080/hero/export?token=' + this.user.token + "&&heroname=" + this.heroname + "&&herosex=" + this.herosex)
},
选择导出
后端:
@GetMapping("/export")
public void exportDate(@RequestParam(required = false) String heroname,
@RequestParam(required = false) String herosex,
@RequestParam(required = false) String ids,
HttpServletResponse response) throws IOException {
ExcelWriter writer= ExcelUtil.getWriter(true);
Iterable<Hero> list;
QueryWrapper<Hero> queryWrapper = new QueryWrapper<Hero>().orderByDesc("id");
// 导出表格
if (StrUtil.isNotBlank(ids)){
// 字符串转数字列表
List<Integer> idsArr= Arrays.stream(ids.split(",")).map(Integer::valueOf).collect(Collectors.toList());
queryWrapper.in("id",idsArr);
}else{
queryWrapper.like(StrUtil.isNotBlank(heroname), "heroname", heroname);
queryWrapper.like(StrUtil.isNotBlank(herosex), "herosex", herosex);
}
list=heroService.list(queryWrapper);
writer.write(list,true);
// 设置响应格式
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
String filename= URLEncoder.encode("特工表","UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");
ServletOutputStream outputStream= response.getOutputStream();
writer.flush(outputStream,true);
outputStream.flush();
outputStream.close();
}
前端:
expertDate () {
if (!this.ids.length) {
// 无选中数据 全部导出
window.open('http://localhost:8080/hero/export?token=' + this.user.token + "&&heroname=" + this.heroname + "&&herosex=" + this.herosex)
} else {
// 转字符串
this.ids.join(',')
window.open('http://localhost:8080/hero/export?token=' + this.user.token + "&&heroname=" + this.heroname + "&&herosex=" + this.herosex + "&&ids=" + this.ids)
}
},
如果项目没有设置token权限的话直接写地址即可: window.open('http://localhost:8080/hero/export' )
若想导出的表格表头是中文,可以在后端给实体类加别名:
@Alias("序号")
private int id;
导出
后端:
/**
* 批量导入
* @param file 传入的excel文件对象
* @return 导入结果
*/
@PostMapping("/import")
public Result importDate(MultipartFile file) throws IOException{
ExcelReader reader=ExcelUtil.getReader(file.getInputStream());
List<Hero> heroList = reader.readAll(Hero.class);
// 写入数据库
try{
heroService.saveBatch(heroList);
}catch (Exception e){
return Result.error("导入出错了!");
}
return Result.success();
}
前端:
<el-upload
action="http://localhost:8080/hero/import"
:headers="{token:user.token}"
style="display:inline-block;margin-left:10px"
:show-file-list="false"
:on-success="handleImport"
>
<el-button type="primary" plain>批量导入</el-button>
</el-upload>
// 导入方法
handleImport (res, file, fileList) {
if (res.code = '200') {
this.$message.success("操作成功")
this.load(1)
} else {
this.$message.error(res.msg)
}
},