easyExcel 实现 导入 导出功能

easyExcel 实现 导入 导出功能

1.导包

<!-- easyexcel -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.0.5</version>
        </dependency>

2.前端实现

<el-button
            size="small"
            style="background-color: #409eff !important; color: white"
            icon="el-icon-download"
            @click="DaocBtn"
            >导出</el-button
          >
          
<el-button
            size="small"
            style="background-color: #409eff !important; color: white"
            icon="el-icon-upload2"
            @click="DaorBtn"
            >导入</el-button
          >
 // 导出
    DaocBtn() {
      var parms = [];
      var tit = false;
      if (this.xiugai_value.length == 0) {
        parms = this.tableData;
        tit = true;
      } else if (this.xiugai_value.length > 0) {
        parms = this.xiugai_value;
      }
      this.$confirm(
        tit ? "请确认是否导出当前全部数据?" : "请确认是否导出当前所勾选数据?",
        "确认",
        {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          download(parms)
            .then((responseData) => {
              console.log("responseData", responseData);
              this.isExcel("xlsx", "用户信息", responseData);
              if (responseData.code == 100) {
              } else {
                this.message_tip(responseData);
              }
            })
            .catch((error) => {
              this.message_tip(error);
            });
        })
        .catch(() => {});
    }

// 导出
export const download = (search) =>
    request({
        url: '/his/sm/user/download',
        method: 'post',
        responseType: 'blob',
        data: search
    })

isExcel(type, name, data) {
      const link = document.createElement("a");
      const blob = new Blob([data], {
        type: "application/vnd.ms-excel;charset=utf-8",
      });
      link.style.display = "none";
      link.href = URL.createObjectURL(blob);
      link.setAttribute("download", `${name}.${type}`);
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
    
// 导入
    DaorBtn() {
      this.$confirm("导入功能仅支持导入不存在的用户,请确认是否继续", "确认", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {})
        .catch(() => {});
    }

3.后端实现

 /**
     * 导入用户信息
     *
     * @param file 用户信息文件
     * @return ResponseEntity 成功/失败
     */
    @PostMapping("importUserToDatabase")
    @ApiOperation(value = "导入用户信息")
    public ResponseEntity importUserToDatabase(MultipartFile file,HttpServletRequest request, HttpServletResponse response,
                                                @RequestBody HisUserVO hisUserVO) {
        return hisUserService.importUserToDatabase(file, request, response, hisUserVO);
    }
/**
     * 导入用户信息
     *
     * @param file 用户信息文件
     */
    public ResponseEntity importUserToDatabase(MultipartFile file,HttpServletRequest request,HttpServletResponse response,
                                               HisUserVO hisUserVO) {
        //创建工具类时传递class,用于后面比对表头使用
        UserListener userListener = new UserListener();

        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            EasyExcel.read(file.getInputStream(), ExportUserVO.class, userListener).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            
        }

        return ResponseEntity.ok(true);
    }
/**
     * 导出用户信息
     *
     * @param response 相应
     * @param hisUserVO 用户信息
     * @return String ""
     */
    @PostMapping("download")
    @ApiOperation(value = "导出用户信息",notes = "export", produces = "application/octet-stream")
    public String download(@RequestBody List<ExportUserVO> hisUserVO, HttpServletResponse response) {
        hisUserService.export(hisUserVO, response);
        return "";
    }
 /**
     * 导出用户信息
     *
     * @param hisUserVO 用户信息
     * @param response 响应
     */
    public void export(List<ExportUserVO> hisUserVO, HttpServletResponse response) {

        if (CollectionUtils.isEmpty(hisUserVO)) {
            throw new McException("当前没有记录可以导出");
        }

        try {
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment; filename="
                    + URLEncoder.encode("用户信息", "UTF-8") + ".xls");
            List list = JSON.parseArray(JSON.toJSONString(hisUserVO));
            EasyExcel.write(response.getOutputStream(), ExportUserVO.class).sheet().doWrite(list);
        } catch (IOException e) {
            throw new McException(e.getMessage());
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值