添加hutool依赖
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.20</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency>
/** * 导入 * @param file * @return * @throws IOException */ @PostMapping("import") public boolean imp(MultipartFile file) throws IOException { InputStream inputStream = file.getInputStream(); ExcelReader reader = ExcelUtil.getReader(inputStream); List<User> list = reader.readAll(User.class); return userService.saveBatch(list); } /** * 导出 * @param response * @throws IOException */ @GetMapping("export") public void export(HttpServletResponse response) throws IOException { List<User> list = userService.list(); ExcelWriter writer = ExcelUtil.getWriter(true); // writer.addHeaderAlias("username","用户名"); // writer.addHeaderAlias("password", "密码"); // writer.addHeaderAlias("nickname", "昵称"); // writer.addHeaderAlias("email", "邮箱"); // writer.addHeaderAlias("phone", "电话"); // writer.addHeaderAlias("address", "地址"); // writer.addHeaderAlias("createTime", "创建时间"); // writer.addHeaderAlias("avatarUrl", "头像"); // 一次性写出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(); }
热门文章
- 深圳动物防疫站给猫打疫苗要钱吗(深圳动物防疫站给猫打疫苗要钱吗现在)
- 开宠物店坑在哪?(开宠物店的坑)
- 领养宠物后要办什么手续呢(领养宠物需要哪些手续)
- 3月9日→21.6M/S|2025年最新免费节点V2ray/SSR/Clash/Shadowrocket订阅链接地址
- 3月5日→22.9M/S|2025年最新免费节点Clash/V2ray/Shadowrocket/SSR订阅链接地址
- 宠物领养公告怎么写(宠物领养公告怎么写好)
- 4月8日→18.8M/S|2025年最新免费节点Clash/SSR/Shadowrocket/V2ray订阅链接地址
- 分布式服务请求报错:Read timed out executing POST http://businessApi/app/app/login
- @Transactional 详解
- 全国最便宜的进货平台玩具店(全国最便宜的玩具批发网站)