1. /**
    2. ● 导出文件
    3. ● @param data 导出的二进制流
    4. ● @param name 文件名
    5. */
    6. export function exportExcel(data:any, name: string, istype = true) {
    7. const blob = new Blob([data], { type: 'application/x-download' });
    8. const objectUrl = URL.createObjectURL(blob);
    9. // window.open(this.objectUrl);
    10. // 用以下方法防止被浏览器拦截,模拟点击
    11. const a = document.createElement('a');
    12. document.body.appendChild(a);
    13. a.setAttribute('id', 'download');
    14. a.setAttribute('style', 'display:none');
    15. a.setAttribute('href', objectUrl);
    16. const filename = ${name}${istype ? '.xlsx' : ''};
    17. a.setAttribute('download', filename);
    18. // ie下载
    19. const uA = window.navigator.userAgent;
    20. const isIE =
    21. /msie\s|trident/|edge//i.test(uA) &&
    22. !!('uniqueID' in document || 'documentMode' in document || 'ActiveXObject' in window || 'MSInputMethodContext' in window);
    23. if (isIE) {
    24. // 兼容IE11无法触发下载的问题
    25. navigator.msSaveBlob(blob, name);
    26. } else {
    27. a.click();
    28. }
    29. // 触发下载后再释放链接
    30. a.addEventListener('click', function () {
    31. URL.revokeObjectURL(objectUrl);
    32. document.getElementById('download')?.remove();
    33. });
    34. }