How to export Javascript array data to excel on the client side

丶灬走出姿态 提交于 2020-07-09 19:12:05

问题


I have been struggling with Javascript array data to put it in excel on the client side. I have read this,How to export JavaScript array info to csv (on client side)?

But that is for CSV. I want Excel.


回答1:


I got a good answer. It may help someone out there

function excelformat() {
        var result_table = [
            ["Day", "Month", "Year"],
            ["1", "January", "2016"],
            ["2", "February", "2016"],
            ["3", "March", "2016"],
            ["4", "April", "2016"],
        ];
        var lineArray = [];
        result_table.forEach(function(infoArray, index) {
            var line = infoArray.join(" \t");
            lineArray.push(index == 0 ? line : line);
        });
        var csvContent = lineArray.join("\r\n");
        var excel_file = document.createElement('a');
        excel_file.setAttribute('href', 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(csvContent));
        excel_file.setAttribute('download', 'Visitor_History.xls');
        document.body.appendChild(excel_file);
        excel_file.click();
        document.body.removeChild(excel_file);
    }


来源:https://stackoverflow.com/questions/47447647/how-to-export-javascript-array-data-to-excel-on-the-client-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!