如何从Javascript生成Excel工作表

时间:2014-06-06 20:53:39

标签: javascript jquery json excel

我们如何使用Json对象的数据生成Excel工作表并生成弹出窗口供下载。

1 个答案:

答案 0 :(得分:0)

您可以使用Excel Builder框架在Firefox中运行良好,不确定IE 9 ...

可能是这样的......

var jsonData = [
    ['junk', 'data', 'for'],
    ['generating', 'excel', 'in'],
    ['java', 'script', ' ']
    ];

require(['excel-builder.js/excel-builder', 'download'], function (EB, downloader) {
    var JSworkBook = EB.createWorkbook();
    var JSworkSheet = JSworkBook.createWorksheet({name: 'Sheet'});

    JSworkSheet.setData(jsonData); 

    JSworkBook.addWorksheet(JSworkSheet);

    var data = EB.createFile(JSworkBook);
    downloader('Artist WB.xlsx', data);
});

您可以使用downloadify生成文件下载

<script type='text/javascript'>
Downloadify.create('domElementId',{
    filename: function(){
        return //The filename
    },
    data: function(){
        return EB.createFile(workbook)
    },
    swf: 'path/to/media/downloadify.swf',
    downloadImage: 'path/to/images/download.png',
    width: 100,
    height: 30,
    transparent: true,
    append: false
});
</script>