hutool工具实现导入导出

时间:2025-03-10 14:16:57
  • /**
  • * 导出接口
  • */
  • @GetMapping("/export")
  • public void export(HttpServletResponse response) throws Exception {
  • // 从数据库查询出所有的数据
  • List<User> list = ();
  • // 通过工具类创建writer 写出到磁盘路径
  • // ExcelWriter writer = (filesUploadPath + "/用户信息.xlsx");
  • // 在内存操作,写出到浏览器
  • ExcelWriter writer = (true);
  • //自定义标题别名
  • ("username", "用户名");
  • ("password", "密码");
  • ("nickname", "昵称");
  • ("email", "邮箱");
  • ("phone", "电话");
  • ("address", "地址");
  • ("createTime", "创建时间");
  • ("avatarUrl", "头像");
  • // 一次性写出list内的对象到excel,使用默认样式,强制输出标题
  • writer.write(list, true);
  • // 设置浏览器响应的格式
  • ("application/;charset=utf-8");
  • String fileName = ("用户信息", "UTF-8");
  • ("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
  • ServletOutputStream out = ();
  • (out, true);
  • out.close();
  • writer.close();
  • }