下载/预览
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' // 重要
})
}
网友评论