首页 > 解决方案 > 超过 3000 个项目导出到 chrome 中的 excel 问题

问题描述

我正在尝试将记录导出到 Excel。在这里,我有超过 3000 条记录。当我的记录低于 3000 条时,它正在导出和保存。但如果我有超过 3000 条记录,我将面临这个问题。保存失败 - 网络错误即将到来。

使用下面的代码将记录导出到 Excel。

function SaveContents(element, filename) {
if (window.Blob && window.navigator.msSaveOrOpenBlob) {
    var blob = new Blob([element], {
        type: 'data:application/vnd.ms-excel'
    });
    window.navigator.msSaveBlob(blob, filename + '.xls');
} else {
    var a = document.createElement('a');
    document.body.appendChild(a);
    a.href = 'data:application/vnd.ms-excel;charset=utf-8,%EF%BB%BF' + encodeURIComponent(element);
    a.download = filename + ".xls";
    a.click();
}}

请给出解决方案。

标签: jquerysharepoint

解决方案


这不再是你应该这样做的方式。不久前,添加了两件事:

  • download<a>用于启动目标下载的属性。例子:

    <a href="myfile.png" download="image.png">Download image</a>
    
  • URL.createObjectURL它将 blob 转换为可以被视为 Internet 上的文件的虚拟 URL,例如:

    // convert letters to bytes
    const letters = new Uint8Array([..."Hello world"].map((x)=>x.charCodeAt(0)));
    const blob = new Blob([letters], {type:"text/plain"});
    // Try to open the result in new tab
    console.log(URL.createObjectURL(blob));
    // remember to clear the URL later using URL.revokeObjectURL
    

因此,请替换设置 URL 的代码部分,如下所示:

var a = document.createElement('a');
document.body.appendChild(a);
var blob = new Blob([element], {
    type: 'data:application/vnd.ms-excel'
});
a.href = URL.createObjectURL(blob);
a.download = filename + ".xls";
a.click();
// lazy cleanup, note that this renders the link invalid
setTimeout(()=>{URL.revokeObjectURL(a.href);}, 500);

推荐阅读