easyexcel与vue配合下载excel

后端

设置响应

// 设置响应头
response.setContentType("application/octet-stream;charset=UTF-8");
String returnName = null;
try {
    returnName = URLEncoder.encode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
}
response.setHeader("Content-Disposition", "attachment;filename=" + returnName);

往response 写入数据

  • 使用easyexcel自带的方法就行
// 这里 指定文件
try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();) {
// try (ExcelWriter excelWriter = EasyExcel.write(fileName).build()) {
    if (hasXL) {
        WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "小料费用差异对比").head(DepreciationEquipXLDto.class).build();
        // 获取小料费用差异
        List<DepreciationEquipXLDto> data2 = assetDepreciationSingleDetailService.getDepreciationEquXLCompare(entityList);
        excelWriter.write(data2, writeSheet2);
    }

    if (hasJL) {
        WriteSheet writeSheet = EasyExcel.writerSheet(0, "其他费用差异对比").head(DepreciationOtherDto.class).build();
        // 获取其他费用差异
        List<DepreciationOtherDto> data = assetDepreciationSingleDetailService.getDepreciationOtherCompare(entityList);
        excelWriter.write(data, writeSheet);

        WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "胶料费用差异对比").head(DepreciationEquipJLDto.class).build();
        // 获取胶料费用差异
        List<DepreciationEquipJLDto> data1 = assetDepreciationSingleDetailService.getDepreciationEquJLCompare(entityList);
        excelWriter.write(data1, writeSheet1);
    }

    WriteSheet writeSheet3 = EasyExcel.writerSheet(3, "总费用差异对比").head(DepreciationTotalDto.class).build();
    // 获取总费用差异
    List<DepreciationTotalDto> data3 = assetDepreciationSingleDetailService.getDepreciationTotalCompare(entityList);
    excelWriter.write(data3, writeSheet3);

    //     获取具体的折旧明细
    WriteSheet writeSheet4 = EasyExcel.writerSheet(4, "折旧明细").head(DepreciationSingleDetailDto.class).build();
    // 获取具体的折旧明细
    List<DepreciationSingleDetailDto> data4 = assetDepreciationSingleDetailService.getDepreciationSingleDetail(entityList);
    excelWriter.write(data4, writeSheet4);

} catch (IOException e) {
    throw new RuntimeException(e);
}

前端

  • 后端返回了之后,有响应数据,但是不会自动下载
  • 前端定义的返回类型必须是blob类型
  • 创建隐式a标签,自动点击下载

export function downloadCompareFile(data) {
  return request({
    url: '/client/depreciationDetail/downloadCompareFile',
    method: 'post',
    data: data,
    responseType: 'blob' // 表明返回服务器返回
  })
}
downloadCompareFile(){
  downloadCompareFile(this.compareDataListParams)
    .then((res) => { // 处理返回的文件流
        const content = res
        const blob = new Blob([content])
        console.log(content)
        const fileName = 'xxx.xlsx'
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        }
      }
      )
},

参考

https://blog.csdn.net/weixin_43597991/article/details/127861599

相关推荐

  1. easyexcelvue配合excel

    2024-03-27 12:00:04       42 阅读
  2. vue 项目实现拉加

    2024-03-27 12:00:04       55 阅读
  3. vue 实现 word 的方式

    2024-03-27 12:00:04       35 阅读
  4. 【springboot】【easyexcelexcel文件读取

    2024-03-27 12:00:04       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-27 12:00:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-27 12:00:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-27 12:00:04       87 阅读
  4. Python语言-面向对象

    2024-03-27 12:00:04       96 阅读

热门阅读

  1. LevelDB

    2024-03-27 12:00:04       39 阅读
  2. ChatGPT编程:让AI成为你的编程助手

    2024-03-27 12:00:04       44 阅读
  3. 软件设计师考试-设计模式速记

    2024-03-27 12:00:04       33 阅读
  4. 数据结构(四)链表实现队列和栈

    2024-03-27 12:00:04       44 阅读
  5. C# 异步和线程的区别

    2024-03-27 12:00:04       38 阅读
  6. SpringBoot+Vue项目跨域问题

    2024-03-27 12:00:04       37 阅读