/json2xls

Primary LanguageJavaScript

json2xls

如果需要尝试进行原生的拼装,参考:https://gist.github.com/xiongsongsong/8b16e39b20734579f10a

npm install json2xls

Example

var json = {
    excel: {
        path: "result.items",
        fileName: "demo.xlsx",
        fields: [
            ["姓名", "name"],
            ["年龄", "age"]
        ]
    },
    result: {
        items: [
            {
                name: "user A",
                age: "29"
            },
            {
                name: "user B",
                age: "35"
            }
        ]
    }
}

var json2xlsx = require('json2xlsx');

app.get('/download', function(){
    //set MIME
    res.header('Content-Type', 'application/octet-stream;charset=utf-8');
    //set Filename
    res.header('Content-Disposition', 'attachment;filename=\"' + encodeURIComponent(excel.fileName) + '\"');

    var buffer = json2xlsx(data);
    res.end(buffer);
});

example

Test

npm test