/**
● 导出文件
● @param data 导出的二进制流
● @param name 文件名
*/
export function exportExcel(data:any, name: string, istype = true) {
const blob = new Blob([data], { type: 'application/x-download' });
const objectUrl = URL.createObjectURL(blob);
// window.open(this.objectUrl);
// 用以下方法防止被浏览器拦截,模拟点击
const a = document.createElement('a');
document.body.appendChild(a);
a.setAttribute('id', 'download');
a.setAttribute('style', 'display:none');
a.setAttribute('href', objectUrl);
const filename = ${name}${istype ? '.xlsx' : ''};
a.setAttribute('download', filename);
// ie下载
const uA = window.navigator.userAgent;
const isIE =
/msie\s|trident/|edge//i.test(uA) &&
!!('uniqueID' in document || 'documentMode' in document || 'ActiveXObject' in window || 'MSInputMethodContext' in window);
if (isIE) {
// 兼容IE11无法触发下载的问题
navigator.msSaveBlob(blob, name);
} else {
a.click();
}
// 触发下载后再释放链接
a.addEventListener('click', function () {
URL.revokeObjectURL(objectUrl);
document.getElementById('download')?.remove();
});
}