javascript 如何将.xlsx数据作为blob保存到文件

djmepvbi  于 2022-12-28  发布在  Java
关注(0)|答案(9)|浏览(397)

我有一个与此问题类似的问题(Javascript: Exporting large text/csv file crashes Google Chrome):
我正在尝试保存 excelbuilder.jsEB.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仍然无法打开。

yb3bgrhw

yb3bgrhw1#

我遇到了和你一样的问题。原来你需要把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;
}
x9ybnkn6

x9ybnkn62#

答案above是正确的。请确保在数据变量中有一个base64格式的字符串数据,没有任何前缀或类似的东西,只是原始数据。
下面是我在服务器端所做的工作(ASP.NET mvc核心):

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();

对我来说效果很好。

u3r8eeie

u3r8eeie3#

我找到了一个对我有效的解决方案:

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();
 };

我只是点了一个响应类型:“斑点”

55ooxyrt

55ooxyrt4#

这一做法的有效期为:https://github.com/SheetJS/js-xlsx版本0.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 });
eit6fx6z

eit6fx6z5#

这是我使用fetch 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);
    });
nwo49xxi

nwo49xxi6#

我的解决方案。
步骤: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 });
}

为我工作。

ubof19bj

ubof19bj8#

这个答案取决于前端和后端都有一个兼容的返回对象,所以要同时给出前端和后端逻辑。确保后端返回数据是base64编码的,这样下面的逻辑才能工作。

// Backend code written in nodejs to generate XLS from CSV
import * as XLSX from 'xlsx';

export const convertCsvToExcelBuffer = (csvString: string) => {
  const arrayOfArrayCsv = csvString.split("\n").map((row: string) => {
    return row.split(",")
  });
  const wb = XLSX.utils.book_new();
  const newWs = XLSX.utils.aoa_to_sheet(arrayOfArrayCsv);
  XLSX.utils.book_append_sheet(wb, newWs);
  const rawExcel = XLSX.write(wb, { type: 'base64' })
  // set res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') 
  // to include content type information to frontend. 
  return rawExcel
}
//frontend logic to get the backend response and download file. 

// function from Ron T's answer which gets a string from ArrayBuffer. 
const 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;
};

const downloadExcelInBrowser = ()=>{
  const excelFileData = await backendCall();
  const decodedFileData = atob(excelFileData.data);
  const arrayBufferContent = s2ab(decodedFileData); // from example above
  const blob = new Blob([arrayBufferContent], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;' });
  var URL = window.URL || window.webkitURL;
  var downloadUrl = URL.createObjectURL(fileBlob);
  if (filename) {
    // use HTML5 a[download] attribute to specify filename
    var a = document.createElement('a');
    // safari doesn't support this yet
    if (typeof a.download === 'undefined') {
      window.location.href = downloadUrl;
    } else {
      a.href = downloadUrl;
      a.download = 'test.xlsx';
      document.body.appendChild(a);
      a.click();
    }
  } else {
    window.location.href = downloadUrl;
  }
}
jtw3ybtb

jtw3ybtb9#

如果你正在使用typescript,那么这里有一个工作示例,说明如何将array转换为xlsx并下载它。

const fileName = "CustomersTemplate";
    const fileExtension = ".xlsx";
    const fullFileName = fileName.concat(fileExtension);

    const workBook : WorkBook = utils.book_new();

    const content : WorkSheet = utils.json_to_sheet([{"column 1": "data", "column 2": "data"}]);

    utils.book_append_sheet(workBook, content, fileName);

    const buffer : any = writeFile(workBook, fullFileName);

    const data : Blob = new Blob(buffer, { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;" });

    const url = URL.createObjectURL(data); //some browser may use window.URL
    
    const a = document.createElement("a");
    document.body.appendChild(a);
    a.href = url;
    a.download = fullFileName;
    a.click();
    
    setTimeout(() => {
      window.URL.revokeObjectURL(url);
      document.body.removeChild(a);
    }, 0);

相关问题