5,007
社区成员




export2Excel() {
let tables = document.getElementById("out-table");
let table_book = this.$XLSX.utils.table_to_book(tables);
var table_write = this.$XLSX.write(table_book, {
bookType: "xlsx",
bookSST: true,
type: "array"
});
try {
let userAgent = navigator.userAgent;
//兼容ie浏览器
if (userAgent.indexOf('Trident') !== -1 && userAgent.indexOf('Windows') !== -1) {
let blob = new Blob([table_write], {type: 'application/octet-stream'});
console.log(blob);
this.$FileSaver.saveAs(blob, "sheetjs.xlsx");
} else { //兼容谷歌 火狐
let file = new File([table_write], "sheetjs.xlsx", {type: "application/octet-stream"});
console.log(file);
this.$FileSaver.saveAs(file);
}
} catch (e) {
if (typeof console !== "undefined") console.log(e, table_write);
}
return table_write;
},
const DownloadBtn = () => {
function createDownload() {
const fileBlob = new Blob([JSON.stringify({
name: 'hello',
title: 'world'
})], {
type: 'application/json;charset=utf-8',
});
const url = window.URL.createObjectURL(fileBlob)
const aLink = document.createElement('a')
aLink.style.display = 'none'
aLink.href = url
aLink.setAttribute('download', 'temp.json')
document.body.appendChild(aLink)
aLink.click()
document.body.removeChild(aLink)
/** 释放掉blob对象 */
window.URL.revokeObjectURL(url)
}
return <button onClick = {() => createDownload()}> 生成并下载文件 </button>
}
function App() {
return (
<div className = "App" >
<DownloadBtn />
</div>
)
}