下載/預覽
getExcel() {
fetchExcel().then(response => {
console.log(response)
console.log(new Blob([response]))
// 下載
// const aLink = document.createElement('a')
// const blob = new Blob([response], { type: 'application/vnd.ms-excel' })
// console.log(blob)
// aLink.href = URL.createObjectURL(blob)
// aLink.setAttribute('download', '哈哈哈哈' + '.xlsx') // 設置下載文件名稱
// aLink.click()
// 返回數據 html 或者json
const fileReader = new FileReader()
fileReader.onload = ev => {
try {
const data = ev.target.result
const workbook = XLSX.read(data, { type: 'binary' })
console.log(workbook)
const firstSheetName = workbook.SheetNames[0]
const worksheet = workbook.Sheets[firstSheetName]
const header = this.getHeaderRow(worksheet)
const results = XLSX.utils.sheet_to_html(worksheet)
// const results = XLSX.utils.sheet_to_json(worksheet)
this.$emit('tablelist', data)
} catch (e) {
return false
}
}
fileReader.readAsBinaryString(response)
})
}
請求
export function fetchExcel(id) {
return request({
url: '/tbTemplate/v1/downloadTemplateFile/7',
method: 'post',
responseType: 'blob' // 重要
})
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。