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

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

本文介绍了如何在客户端将Javascript数组数据导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力将Javascript数组数据放在客户端的excel中.我已阅读此内容,如何导出JavaScript数组信息发送到csv(在客户端)?

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)?

但这是用于CSV的.我想要Excel.

But that is for CSV. I want Excel.

推荐答案

我得到了一个很好的答案.它可能会帮助某个人

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

这篇关于如何在客户端将Javascript数组数据导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 12:18