(hutool工具)Java后端返回前端Excel流文件(3天还没有琢磨出来)

193 阅读1分钟
第一步

前端请求需要带上一个类型: responseType: 'blob',

第二步

后端返回值是空:void

设置response

response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment");

前端接收

new Blob([res],{type:"application/vnd.ms-excel"})

type要和后端的一样,有些人返回的值用的res.data,但是实际上我这边是res

let blogw = new Blob([res], { type: 'application/vnd.ms-excel' })
let link = document.createElement('a')
let objectUrl = window.URL.createObjectURL(blogw)    //创建一个新的url对象
link.href = objectUrl
let file_name = '订单管理列表' + this.getDate() + '.xlsx'
link.download = file_name
link.click()
window.URL.revokeObjectURL(objectUrl)

这边在介绍几个接收类型

//万能

const blob = new Blob([content], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8" });

//xls、xlsx

const blob = new Blob([content], { type: "application/vnd.ms-excel" });

//pdf

const blob = new Blob([content], { type: "application/pdf" });

//zip

const blob = new Blob([content], { type: "application/zip" });