node.js导出excel表格----excel-export

node.js只要安装好excel-export插件之后就可以很方便的将所需要的数据导出为excel表格。
1,在项目目录下安装excel-export插件
npm install excel-export
2,页面
js:$("#exportExcel").click(function(){
console.info("exportExcel");
var id = $("#contractID").val();
console.info("id:"+id);
var url =  "/api/contracts/exportExcel/" + id;
console.info(url);
window.location = url;//这里不能使用get方法跳转,否则下载不成功

});

3,router.js
var nodeExcel = require('excel-export');//关联excel-export模块
var contract = require('../app/controller/contract');
app.get('/api/contracts/exportExcel/:id',contract.exportExcel);//跳转到后台

4,后台
exports.exportExcel = function(req, res) {
 /**静态数据
*  var conf ={};
    conf.cols = [
        {caption:'string', type:'string'},
        {caption:'date', type:'date'},
        {caption:'bool', type:'bool'},
        {caption:'number', type:'number'}               
    ];
    conf.rows = [
        ['pi', (new Date(2013, 4, 1)).getJulian(), true, 3.14],
        ["e", (new Date(2012, 4, 1)).getJulian(), false, 2.7182]
    ];
    var result = nodeExcel.execute(conf);
    res.setHeader('Content-Type', 'application/vnd.openxmlformats');
    res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
    res.end(result, 'binary');

**/

console.log("req.params.id:"+req.params.id);
var queryId = req.params['id']+'';
var contract = new Contract();
var conf = {};
conf.cols = [
   {caption:'采购编号', type:'string'},
   {caption:'合同名称', type:'string'},
   {caption:'甲方', type:'string'},
   {caption:'甲方部门', type:'string'},
   {caption:'乙方', type:'string'},
   {caption:'乙方部门', type:'string'},
   {caption:'签订日期', type:'date'},
   {caption:'中标日期', type:'date'},
   {caption:'结束日期', type:'date'},
   {caption:'销售负责人', type:'string'},
   {caption:'商务负责人', type:'string'},
   {caption:'业绩归属部门', type:'string'},
   {caption:'金额', type:'number'},
   {caption:'状态', type:'string'}

];
var getId = {
_id: queryId
};
contract.checkIdData(getId,function(data){
  console.log("lijuanxia");
  console.log("data.lentht"+data.length);
         
          var m_data = [];
          var arry = [data[0].myId, data[0].name, data[0].partyA, data[0].partyADept, data[0].partyB, data[0].partyBDept, data[0].signDate, data[0].beginDate, data[0].endDate, "销售负责人", "商务负责人", "业绩归属部门", data[0].amount, data[0].state ];
          m_data[0] = arry;
          conf.rows = m_data;
          
          var result = nodeExcel.execute(conf);
         
          res.setHeader('Content-Type', 'application/vnd.openxmlformats');
         
          res.setHeader("Content-Disposition", "attachment; filename=" +data[0].name+ ".xlsx");
      
          res.end(result, 'binary');
});
}

你可能感兴趣的:(node.js导出excel表格----excel-export)