如何在客户端将Javascript数组数据导出到excel

时间:2017-11-23 04:37:46

标签: javascript arrays excel export

我一直在努力使用Javascript数组数据将它放在客户端的excel中。我已经读过这个,How to export JavaScript array info to csv (on client side)?

但这是针对CSV的。我想要Excel。

1 个答案:

答案 0 :(得分:0)

我得到了一个很好的答案。它可以帮助那里的人

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