場(chǎng)景:
使用xlsx包導(dǎo)出excel文件時(shí)埋心,需要把導(dǎo)出的excel同時(shí)上傳到服務(wù)器上去,但是xlsx沒有直接獲取到file文件的方法忙上,所以經(jīng)過請(qǐng)教大神之后對(duì)xlsx下載文件的時(shí)候進(jìn)行了流和file文件的轉(zhuǎn)化拷呆,廢話不多說直接上代碼了
代碼如下:
//Export2Excel.js 文件下
const blob = new Blob([s2ab(wbout)], {
type: "application/octet-stream"
})
saveAs(blob, `${filename}.${bookType}`);
const file = new File([blob], `${filename}.${bookType}`)
return file;
使用時(shí)的代碼
const tHeader = ['id', 'title', 'author', 'pageviews', 'display_time']
const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
let list = [
{
"id": 1,
"author": "Barbara",
"title": "Qlgsldfl",
"display_time": "2001-09-12 07:31:47",
"pageviews": 4458,
}
]
const dataStr = this.formatJson(filterVal, list)
console.log('dataStr',dataStr)
const file = export_json_to_excel({
header: tHeader,
data: dataStr,
filename: data.templateName,
autoWidth: true,
bookType: 'xlsx'
})
// 拿到file對(duì)象后 調(diào)用上傳接口,然后服務(wù)器返回的上傳后的文件地址后疫粥,就可以繼續(xù)處理后續(xù)邏輯
console.log(file)
下篇介紹blob轉(zhuǎn)file文件