springboot vue 批量导出
第一步
@GetMapping("/export")
@ApiOperation("导出")
public void export(HttpServletResponse response) throws Exception {
// 从数据库查询出所有的数据
List<Notice> list = noticeService.list();
// 在内存操作,写出到浏览器
ExcelWriter writer = ExcelUtil.getWriter(true);
//自定义标题别名
// writer.addHeaderAlias("nName", "公告名称");
// writer.addHeaderAlias("nContent", "公告内容");
// 一次性写出list内的对象到excel,使用默认样式,强制输出标题
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 out = response.getOutputStream();
writer.flush(out, true);
out.close();
writer.close();
}
第二步vue中添加按钮
<el-button style="margin-left: 10px" type="primary" @click="exportNotice">导出 <i class="el-icon-top"></i></el-button>
第三步vue方法创建
//导出
exportNotice(){
window.open("http://localhost:8080/notice/export")
},