如何将.xlsx数据保存为Blob文件 [英] How to save .xlsx data to file as a blob

查看:166
本文介绍了如何将.xlsx数据保存为Blob文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此问题也有类似的疑问( JavaScript:导出大文本/csv文件会使Google Chrome崩溃):

I have a similar question to this question(Javascript: Exporting large text/csv file crashes Google Chrome):

我正在尝试保存由 excelbuilder.js EB.createFile()函数创建的数据.如果将文件数据作为链接的 href 属性值,它将起作用.但是,当数据量很大时,它将使Chrome浏览器崩溃.代码是这样的:

I am trying to save the data created by excelbuilder.js's EB.createFile() function. If I put the file data as the href attribute value of a link, it works. However, when data is big, it crashes Chrome browser. Codes are like this:

//generate a temp <a /> tag
var link = document.createElement("a");
link.href = 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,' + encodeURIComponent(data);
link.style = "visibility:hidden";
link.download = fileName;

document.body.appendChild(link);
link.click();
document.body.removeChild(link);

我使用excelbuilder.js创建数据的代码如下:

My codes to create the data using excelbuilder.js is like follows:

var artistWorkbook = EB.createWorkbook();
var albumList = artistWorkbook.createWorksheet({name: 'Album List'});

albumList.setData(originalData); 

artistWorkbook.addWorksheet(albumList);

var data = EB.createFile(artistWorkbook);

根据类似问题的答案( JavaScript:导出大型文本/csv文件,Google Chrome崩溃),需要创建一个Blob.

As suggested by the answer of the similar question (Javascript: Exporting large text/csv file crashes Google Chrome), a blob needs to be created.

我的问题是,文件中保存的内容不是可以由Excel打开的有效Excel文件.我用于保存 blob 的代码如下:

My problem is, what is saved in the file isn't a valid Excel file that can be opened by Excel. The codes that I use to save the blob is like this:

var blob = new Blob(
    [data],
    {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,"}
);

// Programatically create a link and click it:
var a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = fileName;
a.click();

如果我将上述代码中的 [data] 替换为 [Base64.decode(data)] ,则文件中保存的内容看起来更像预期的excel数据,但仍无法通过Excel打开.

If I replace the [data] in the above codes with [Base64.decode(data)], the contents in the file saved looks more like the expected excel data, but still cannot be opened by Excel.

谢谢!

推荐答案

我遇到了与您相同的问题.事实证明,您需要将Excel数据文件转换为ArrayBuffer.

I had the same problem as you. It turns out you need to convert the Excel data file to an ArrayBuffer.

var blob = new Blob([s2ab(atob(data))], {
    type: ''
});

href = URL.createObjectURL(blob);

s2ab(字符串到数组缓冲区)方法(我从获得)https://github.com/SheetJS/js-xlsx/blob/master/README.md )是:

The s2ab (string to array buffer) method (which I got from https://github.com/SheetJS/js-xlsx/blob/master/README.md) is:

function s2ab(s) {
  var buf = new ArrayBuffer(s.length);
  var view = new Uint8Array(buf);
  for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  return buf;
}

这篇关于如何将.xlsx数据保存为Blob文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆