我对这个问题有一个类似的问题(Javascript: Exporting large text/csv file crashes Google Chrome):
我正在尝试保存由 excelbuilder.js 的EB.createFile()
函数创建的数据。如果我将文件数据作为链接的href
属性值,它就可以工作。但是,当数据很大时,它会崩溃Chrome浏览器。代码是这样的:
//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创建数据的代码如下:
var artistWorkbook = EB.createWorkbook();
var albumList = artistWorkbook.createWorksheet({name: 'Album List'});
albumList.setData(originalData);
artistWorkbook.addWorksheet(albumList);
var data = EB.createFile(artistWorkbook);
正如类似问题(Javascript: Exporting large text/csv file crashes Google Chrome)的回答所示,需要创建一个blob。
我的问题是,文件中保存的内容不是Excel可以打开的有效Excel文件。我用来保存blob
的代码是这样的:
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打开。
谢谢!
答案 0 :(得分:22)
我和你有同样的问题。事实证明,您需要将Excel数据文件转换为ArrayBuffer。
var blob = new Blob([s2ab(atob(data))], {
type: ''
});
href = URL.createObjectURL(blob);
s2ab(字符串到数组缓冲区)方法(我从https://github.com/SheetJS/js-xlsx/blob/master/README.md获得)是:
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;
}
答案 1 :(得分:6)
答案above是正确的。请确保您在数据变量中的base64中有一个字符串数据,没有任何前缀或类似原始数据的东西。
这是我在服务器端做的(asp.net mvc core):
string path = Path.Combine(folder, fileName);
Byte[] bytes = System.IO.File.ReadAllBytes(path);
string base64 = Convert.ToBase64String(bytes);
在客户端,我做了以下代码:
const xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.setRequestHeader("Content-Type", "text/plain");
xhr.onload = () => {
var bin = atob(xhr.response);
var ab = s2ab(bin); // from example above
var blob = new Blob([ab], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;' });
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = 'demo.xlsx';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
xhr.send();
它对我来说非常适合。
答案 2 :(得分:2)
尝试FileSaver.js
库。它可能有所帮助。
答案 3 :(得分:1)
此版本自https://github.com/SheetJS/js-xlsx的v0.14.0起生效
/* generate array buffer */
var wbout = XLSX.write(wb, {type:"array", bookType:'xlsx'});
/* create data URL */
var url = URL.createObjectURL(new Blob([wbout], {type: 'application/octet-stream'}));
/* trigger download with chrome API */
chrome.downloads.download({ url: url, filename: "testsheet.xlsx", saveAs: true });
答案 4 :(得分:1)
这是我使用提取API的实现。服务器端点发送一个字节流,而客户端接收一个字节数组并从中创建一个blob。然后将生成一个.xlsx文件。
return fetch(fullUrlEndpoint, options)
.then((res) => {
if (!res.ok) {
const responseStatusText = res.statusText
const errorMessage = `${responseStatusText}`
throw new Error(errorMessage);
}
return res.arrayBuffer();
})
.then((ab) => {
// BE endpoint sends a readable stream of bytes
const byteArray = new Uint8Array(ab);
const a = window.document.createElement('a');
a.href = window.URL.createObjectURL(
new Blob([byteArray], {
type:
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
}),
);
a.download = `${fileName}.XLSX`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
})
.catch(error => {
throw new Error('Error occurred:' + error);
});
答案 5 :(得分:1)
我找到了一个适合我的解决方案:
const handleDownload = async () => {
const req = await axios({
method: "get",
url: `/companies/${company.id}/data`,
responseType: "blob",
});
var blob = new Blob([req.data], {
type: req.headers["content-type"],
});
const link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = `report_${new Date().getTime()}.xlsx`;
link.click();
};
我只是指出一个 responseType: "blob"
答案 6 :(得分:0)
对我来说解决方案。
步骤:1
<a onclick="exportAsExcel()">Export to excel</a>
步骤:2
我正在使用文件存储库。
了解更多:https://www.npmjs.com/package/file-saver
npm i file-saver
步骤:3
let FileSaver = require('file-saver'); // path to file-saver
function exportAsExcel() {
let dataBlob = '...kAAAAFAAIcmtzaGVldHMvc2hlZXQxLnhtbFBLBQYAAAAACQAJAD8CAADdGAAAAAA='; // If have ; You should be split get blob data only
this.downloadFile(dataBlob);
}
function downloadFile(blobContent){
let blob = new Blob([base64toBlob(blobContent, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')], {});
FileSaver.saveAs(blob, 'report.xlsx');
}
function base64toBlob(base64Data, contentType) {
contentType = contentType || '';
let sliceSize = 1024;
let byteCharacters = atob(base64Data);
let bytesLength = byteCharacters.length;
let slicesCount = Math.ceil(bytesLength / sliceSize);
let byteArrays = new Array(slicesCount);
for (let sliceIndex = 0; sliceIndex < slicesCount; ++sliceIndex) {
let begin = sliceIndex * sliceSize;
let end = Math.min(begin + sliceSize, bytesLength);
let bytes = new Array(end - begin);
for (var offset = begin, i = 0; offset < end; ++i, ++offset) {
bytes[i] = byteCharacters[offset].charCodeAt(0);
}
byteArrays[sliceIndex] = new Uint8Array(bytes);
}
return new Blob(byteArrays, { type: contentType });
}
为我工作。 ^^