How to export Javascript array data to excel on the client side - Stack Overflow

I have been struggling with Javascript array data to put it in excel on the client side. I have read th

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.

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.

Share Improve this question asked Nov 23, 2017 at 4:37 kimoduorkimoduor 6148 silver badges17 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744706575a4589089.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信