EasyExcel最简洁版导出excel,前后端java+vue

后端部分:

1、导出依赖:

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.11</version>
        </dependency>

2、controller层代码

    @RequestMapping("/exportExcel")
    @ResponseBody
    public void exportExcel(@RequestBody CsLawsuitOrderDTO dto, HttpServletResponse response) {

        ServerValidUtils.validObj(dto, "参数不允许为空");
        // 查询所有数据,最多下载2000条
        dto.setPageSize(2000);
        IPage<CsLawsuitOrder> page = csLawsuitOrderService.queryByPage(dto);
        ServerValidUtils.valid(page == null || page.getTotalPageCount() == 0, "没有数据,请重新选择筛选条件");
        ServerValidUtils.valid(page.getTotalPageCount() > 2000, "数据超过2000条,请重新选择筛选条件");
        List<CsLawsuitOrder> data = (List<CsLawsuitOrder>) page.getData();
        try {
            response.reset();
            String fileName = URLEncoder.encode("诉讼列表".concat(DateTimeUtil.format(new Date(),
                                                                                  DateTimeUtil.pattern_ymd_hms)).concat(ExcelTypeEnum.XLSX.getValue()),
                                                "UTF-8");
            response.setHeader("Content-disposition", "attachment; filename=" + fileName);
            response.setContentType("application/msexcel");
            response.setCharacterEncoding("ISO-8859-1");

            try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) {
                EasyExcel.write(response.getOutputStream(), CsLawsuitOrder.class).sheet("诉讼列表").doWrite(data);
                bos.flush();
            }
        } catch (Exception e) {
            logger.error("下载文件出现异常,异常信息为", e);
        }
    }

前端接收:

    //导出Excel数据
    exportCur() {
        // 简易版
      // window.open(
      //   `${location.origin}${
      //     process.env.VUE_APP_BASE_API
      //   }/lawsuitOrder/exportExcel?${qs.stringify({ ...this.form })}`
      // );
      // window.location = `${location.origin}${
      //   process.env.VUE_APP_BASE_API
      // }/lawsuitOrder/exportExcel?${qs.stringify({ ...this.form })}`;

        // axios接收版
      if (this.btnList[2].title === "导出excel中...") {
        this.$modal.msg("导出excel中,请勿重复点击!");
        return;
      }
      this.btnList[2].title = "导出excel中...";
      this.$store
        .dispatch("lawsuitBatchList/ExportExcel", { ...this.form })
        .then((data) => {
          // let blob = new Blob([data]);
          const blobUrl = window.URL.createObjectURL(data);
          const link = document.createElement("a");
          document.body.appendChild(link);
          link.style.display = "none";
          link.href = blobUrl;
          link.download = `诉讼列表${dayjs(Date.now()).format(
            "YYYY-MM-DD HH:mm:ss"
          )}.xlsx`;
          link.click();
          document.body.removeChild(link);
          window.URL.revokeObjectURL(blobUrl);
          this.btnList[2].title = "待诉导出";
        })
        .catch(() => {
          this.btnList[2].title = "待诉导出";
        });
    },
// 导出全部excel
export function exportExcel(options) {
  return axios({
    method: 'post',
    url: baseURL+`${process.env.VUE_APP_OLD_APP}/lawsuitOrder/exportExcel`,
    responseType: 'blob',     //设置返回信息为二进制文件,默认为json
    data: JSON.stringify(options)   // //后台照常用@RequestBody接收即可
  })
}

ps:前端dayjs工具包函数需要自己导入packge.json里面加一个 "dayjs": "^1.10.7" 的依赖;   import dayjs from "dayjs"

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值