用于写入excel文件的节点XLSX

时间:2016-10-03 05:27:17

标签: node.js excel mongodb

我尝试使用exceljs来创建mongodb集合的excel文件。 这就是我想要做的 这是我的mongodb collecton

{
  "id": "01",
  "make": "toyota",
  "year": [
    2005,
    2006,
    2007,
    2008,
    2009,
    2010
   ],
  "model": "fortuner",
  "type": "a"
} 
{

"id": "02",
"make": "toyota",
"year": [
    2005,
    2006,
    2007,
    2008,
    2009,
    2010
   ],
"model": "land cruiser 200",
"type": "b"
} 

{
"id": "03",
"make": "toyota",
"year": [
    2005,
    2006,
    2007,
    2008,
    2009,
    2010
],
"model": "land cruiser 200",
"type": "e"
}

,我想从这个集合中创建excel文件

ID生成年份模型类型

01 toyota 2005-2010 a xxxx

02日产2000-2006 b xxxx

在XLSX官方文档中,他们说要使用

编写工作簿

nodejs写入文件:

/* output format determined by filename */
XLSX.writeFile(workbook, 'out.xlsx');
/* at this point, out.xlsx is a file that you can distribute */
write to binary string (using FileSaver.js):
/* bookType can be 'xlsx' or 'xlsm' or 'xlsb' */
var wopts = { bookType:'xlsx', bookSST:false, type:'binary' };

var wbout = XLSX.write(workbook,wopts);

function s2ab(s) {
 var buf = new ArrayBuffer(s.length);
 var view = new Uint8Array(buf);
 for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
 return buf;
}

/* the saveAs call downloads a file on the local machine */
saveAs(new Blob([s2ab(wbout)],{type:""}), "test.xlsx")

我无法理解如何使用它。 有谁知道怎么做?

2 个答案:

答案 0 :(得分:1)

看这段代码:

            const reader = require('xlsx');

            const json = [
                {
                    id: 1,
                    color: 'red',
                    number: 75
                },
                {
                    id: 2,
                    color: 'blue',
                    number: 62
                },
                {
                    id: 3,
                    color: 'yellow',
                    number: 93
                },
            ];

            let workBook = reader.utils.book_new();
            const workSheet = reader.utils.json_to_sheet(json);
            reader.utils.book_append_sheet(workBook, workSheet, `response`);
            let exportFileName = `response.xls`;
            reader.writeFile(workBook, exportFileName);

我在这里找到了这篇文章:https://buildcoding.com/useful-npm-libraries-to-create-excel-files-in-node-js/#3_xlsx

答案 1 :(得分:0)

使用excel-export。这是代码段。

        var nodeExcel = require('excel-export');
        app.get('/downloadExcel',function(req,res){
        var configuration = {};
        configuration.cols=[{
                            caption:'ID',
                            type:'String',
                            width:20
                        },{
                            caption:'Make',
                            type:'String',
                            width:20

                        },{
                            caption:'Year',
                            type:'String',
                            width:20
                        },{
                            caption:'Model',
                            type:'String',
                            width:20
                         },{
                            caption:'Type',
                            type:'String',
                            width:20
    }
    ];
        });

configuration.rows  = [["01", "toyota" ,"2005-2010", "a", "xxxx"],["02", "nissan" ,"2005-2010", "b", "xxxx"]];
var result=nodeExcel.execute(configuration);
res.setHeader('Content-Type','application/vnd.openxmlformates');
res.setHeader("Content-Disposition","attachment;filename="+"file_name.xlsx");
res.end(result,'binary');