JavaScript极好的Export Blob问题



我有一个网页,该网页生成了一个大约1000-5000记录(14列)的报告。代码:

excel: function(anchor, table, name) {
        table = get(table);
        var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
        var hrefvalue = uri.excel + base64(format(template.excel, ctx));
        anchor.href = hrefvalue;
        // Return true to allow the link to work
        return true;
    },

如果记录约为1500,则可以使用,但如果超过2000,则没有。我看到了这篇文章如何用10000到40000行的Excel导出表,并尝试将其从我的代码中合并:

var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
var blob = b64toBlob(ctx, "application/vnd.ms-excel");
var blobUrl = URL.createObjectURL(blob);
window.location = blobUrl;
function b64toBlob(b64Data, contentType, sliceSize) {
    contentType = contentType || '';
    sliceSize = sliceSize || 512;
    var byteCharacters = atob(b64Data);
    var byteArrays = [];
    for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
        var slice = byteCharacters.slice(offset, offset + sliceSize);
        var byteNumbers = new Array(slice.length);
        for (var i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
        }
        var byteArray = new Uint8Array(byteNumbers);
        byteArrays.push(byteArray);
    }
    var blob = new Blob(byteArrays, {type: contentType});
    return blob;
}

我现在可以用2000多个记录导出记录,但是当我打开电子表格时,它还将菜单,报告按钮,文本框导出到Excel中,我需要的报告的开始将在第150行中使用所有内容出口的额外东西。

是否可以在Excel中删除被输出的UI?

我将在黑暗中拍摄 - 对您的代码的工作方式并不了解... get(), format(), template.excel对许多人来说是不知道的,因为我们很难很难帮助您...

,但这是我认为您必须做的:

excel: function(anchor, table, name) {
  table = get(table);
  var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
  var data = format(template.excel, ctx)
  var blob = new Blob([data], {type: "application/vnd.ms-excel"})
  var url = URL.createObjectURL(blob)
  anchor.href = url;
  anchor.download = 'file.xlsx'
  // Return true to allow the link to work
  return true;
}

顺便说一句,尝试完全拧紧base64-这是不良练习

我推荐的另一件事是filesaver.js

最新更新