Exporting an array to excel file with cell formatting

前端 未结 1 1457
孤街浪徒
孤街浪徒 2021-01-02 19:04

I\'m currently trying to export an array to an excel file with cell formatting.

I\'m starting off with this code here:

https://github.com/SheetJS/js-         


        
1条回答
  •  孤城傲影
    2021-01-02 19:09

    You base your code on a node.js test. The documentation states:

    Writing Workbooks

    For writing, the first step is to generate output data. The helper functions write and writeFile will produce the data in various formats suitable for dissemination. The second step is to actual share the data with the end point. Assuming workbook is a workbook object:

    nodejs write to file:

    /* output format determined by filename */
    XLSX.writeFile(workbook, 'out.xlsx');
    /* at this point, out.xlsx is a file that you can distribute */
    

    write to binary string (using FileSaver.js):

    /* bookType can be 'xlsx' or 'xlsm' or 'xlsb' */
    var wopts = { bookType:'xlsx', bookSST:false, type:'binary' };
    
    var wbout = XLSX.write(workbook,wopts);
    
    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;
    }
    
    /* the saveAs call downloads a file on the local machine */
    saveAs(new Blob([s2ab(wbout)],{type:""}), "test.xlsx")
    

    So to sum up: You try to use node.js internal functions in the browser, which fails. If you try to follow the seconds approach ( XLSX.write() instead of XLSX.writeFile()), you should be fine.

    0 讨论(0)
提交回复
热议问题