@GetMapping("/exportScore") public void exportScore(HttpServletResponse response) throws Exception { // 从数据库查询出所有的数据 List<Score> list = scoreService.getScoreAll(); // 通过工具类创建writer 写出到磁盘路径 // ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用户信息.xlsx"); // 在内存操作,写出到浏览器 ExcelWriter writer = ExcelUtil.getWriter(true); //自定义标题别名 writer.addHeaderAlias("id", "用户名"); writer.addHeaderAlias("exercise_id", "运动名称"); writer.addHeaderAlias("score", "评分值"); writer.addHeaderAlias("time", "评分时间"); // 一次性写出list内的对象到excel,使用默认样式,强制输出标题 writer.write(list, true); // 设置浏览器响应的格式 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"); // 对文件名进行URL编码,解决中文文件名乱码问题 String fileName = URLEncoder.encode("运动质量评价信息", "UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx"); // 获取响应的输出流 ServletOutputStream out = response.getOutputStream(); // 将excel内容写出到响应流 writer.flush(out, true); // 关闭输出流和writer out.close(); writer.close(); }
标签:实体,写入,writer,导出,list,addHeaderAlias,true,response,out From: https://www.cnblogs.com/stevenduxiang/p/18152756