添加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(); }
热门文章
- 动物医院诊疗许可证办理条件及流程图片高清(动物医院诊疗许可证办理条件及流程图片高清)
- 动物防疫疫苗管理工作总结范文(动物防疫工作总结及下步工作开展)
- 2月15日最新机场订阅 | 18.5M/S|2025年Shadowrocket/Clash/SSR/V2ray免费节点地址链接分享
- 反射 三种获得class对象的方式
- 宠物用疫苗可以带上高铁吗北京地区(宠物疫苗能上地铁吗)
- 领养宠物赚钱软件是哪个公司的软件(领养宠物的app有哪些)
- 1月11日最新机场订阅 | 18.2M/S|2025年Clash/Shadowrocket/SSR/V2ray免费节点地址链接分享
- 2月7日最新机场订阅 | 22.2M/S|2025年Clash/V2ray/Shadowrocket/SSR免费节点地址链接分享
- 家养的布偶猫能买吗(家养布偶猫价格多少钱一只)
- 动物疫苗接种时应注意哪些事项(动物疫苗使用注意事项)