前台如何将json 数据导出到excel表格中

PuckLin 发布于 2015/12/22 17:05
阅读 16K+
收藏 1
    客户 有个需求 要我们将服务器端返回的一堆json 数据 导出变成excel文件,网上搜索了,然并没有什么好的解决办法(也有可能是我没找到),特在此求教大神。有没人做过相类似的项目?怎么解决?(开发框架用的是angularjs,里面支持原生的js和jquery(少用))。
加载中
2
Java_Coder
Java_Coder
对JS不是很了解,用Google帮你搜了一下。   看看能不能解决你的问题。


How to create CSV or EXCEL file from JSON via Javascript

Hi there, Creating an Excel or CSV file through Javascript/Jquery is very easy as well as fast, because you don't need to postback your page to generate CSV/Excel file.
You just need a some knowledge about JSON. Nowadays JSON going very popular for process the data fast and easily.
Here is the sample code for generating the csv/excel file  and follow the live preview at the end of the code.
function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {
    //If JSONData is not an object then JSON.parse will parse the JSON string in an Object
    var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;

    var CSV = '';
    //Set Report title in first row or line

    CSV  = ReportTitle   '\r\n\n';

    //This condition will generate the Label/Header
    if (ShowLabel) {
        var row = "";

        //This loop will extract the label from 1st index of on array
        for (var index in arrData[0]) {

            //Now convert each value to string and comma-seprated
            row  = index   ',';
        }

        row = row.slice(0, -1);

        //append Label row with line break
        CSV  = row   '\r\n';
    }

    //1st loop is to extract each row
    for (var i = 0; i < arrData.length; i  ) {
        var row = "";

        //2nd loop will extract each column and convert it in string comma-seprated
        for (var index in arrData[i]) {
            row  = '"'   arrData[i][index]   '",';
        }

        row.slice(0, row.length - 1);

        //add a line break after each row
        CSV  = row   '\r\n';
    }

    if (CSV == '') {
        alert("Invalid data");
        return;
    }

    //Generate a file name
    var fileName = "MyReport_";
    //this will remove the blank-spaces from the title and replace it with an underscore
    fileName  = ReportTitle.replace(/ /g, "_");

    //Initialize file format you want csv or xls
    var uri = 'data:text/csv;charset=utf-8,'   escape(CSV);

    // Now the little tricky part.
    // you can use either>> window.open(uri);
    // but this will not work in some browsers
    // or you will not get the correct file extension    

    //this trick will generate a temp <a /> tag
    var link = document.createElement("a");
    link.href = uri;

    //set the visibility hidden so it will not effect on your web-layout
    link.style = "visibility:hidden";
    link.download = fileName   ".csv";

    //this part will append the anchor tag and remove it after automatic click
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}



1
Java_Coder
Java_Coder

还有:::

https://github.com/rikkertkoppes/json2xls

npm install json2xls



var json2xls = require('json2xls');
var json = {
    foo: 'bar',
    qux: 'moo',
    poo: 123,
    stux: new Date()
}

var xls = json2xls(json);

fs.writeFileSync('data.xlsx', xls, 'binary');


var jsonArr = [{
    foo: 'bar',
    qux: 'moo',
    poo: 123,
    stux: new Date()
},
{
    foo: 'bar',
    qux: 'moo',
    poo: 345,
    stux: new Date()
}];

app.use(json2xls.middleware);

app.get('/',function(res) {
    res.xls('data.xlsx', jsonArr);
});





Java_Coder
Java_Coder
好的
P
PuckLin
多谢您的回答,我代入实际项目中试一下!!!
0
maradona
maradona
为啥不服务端直接生成excel呢,客户的需求可能并不一定要求把json转成excel吧...
0
P
PuckLin

引用来自“Java_Coder”的评论

还有:::

https://github.com/rikkertkoppes/json2xls

npm install json2xls



var json2xls = require('json2xls');
var json = {
    foo: 'bar',
    qux: 'moo',
    poo: 123,
    stux: new Date()
}

var xls = json2xls(json);

fs.writeFileSync('data.xlsx', xls, 'binary');


var jsonArr = [{
    foo: 'bar',
    qux: 'moo',
    poo: 123,
    stux: new Date()
},
{
    foo: 'bar',
    qux: 'moo',
    poo: 345,
    stux: new Date()
}];

app.use(json2xls.middleware);

app.get('/',function(res) {
    res.xls('data.xlsx', jsonArr);
});





这个好用点  npm 导入 更切合我的项目
0
风欲静而树不止
说到底还是导出excel,后台完成不就好了
0
FebruaryFeb
FebruaryFeb

SpreadJS控件可以直接导入导出Json,导入导出Excel,参考以下的Demo,这些问题都可以得到解决:

http://gcdn.gcpowertools.com.cn/showtopic-21245-1-1.html

0
陈嘉镇

这里有个在线小工具,可以解决这个问题。

http://j2e.kpoda.com/

返回顶部
顶部