jquery - 超过 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();
}}
请给出解决方案。
解决方案
这不再是你应该这样做的方式。不久前,添加了两件事:
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);
推荐阅读
- python - 基于列表的 Python Pandas Dataframe 子集
- python - 简单平滑数据的功能
- tesseract - 使用 tessarct 3.04 版解决验证码
- python - 在 Azure 中使用 python 获取角色分配列表
- javascript - Javascript - 将变量传递给 url
- python-3.x - 遍历图中所有可用路径
- swift - Swift 包管理器:“多个目标命名...”
- arrays - 用r中的循环计算数组中的对
- javascript - 如何只允许有 2 位小数的数字
- mobile-robot-toolkit - 如何从 Rawlog 文件中以 txt 格式进行 3d 扫描?