2016-09-30 48 views
0

在我的流星的應用程序,我有一個集合名稱的車輛,我想該集合的Excel文件出口MongoDB的數據導入Excel中使用按鈕,點擊

{ 
    "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豐田2005-2010一XXXX

02 XXX XXXX XXX XXXXXX

我該如何在流星中做到這一點?

回答

0

我這樣做的項目。 我已經使用exceljs這是一個允許創建excel文件的npm庫。 當用戶點擊下載按鈕時,我創建了一個服務器端路由來調用。

Router.route('/export/cart', { where: 'server' }).post(function() { 
    var body = this.request.body; 
    var cart = Cart.findOne({_id: body.cartId}); 
    var user = Meteor.users.findOne({_id: body.userId}); 

    var Excel = Meteor.npmRequire('exceljs'); 
    var workbook = new Excel.Workbook(); 
    var attachmentFilename = cart.title+" "+moment().format('DD-MM-YYYY')+".xlsx"; 
    this.response.writeHead(200, { 
    'Content-Type':"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
    'Content-Disposition': 'attachment; filename=' + attachmentFilename 
    }); 

    /* your logic to fill excel file check exceljs for doc*/ 

    /*then write responde */ 
    workbook.xlsx.write(this.response); 
}); 

這個程序,它已經開發之前流星1.3所以它使用NpmRequire,但這COSE它只是得到的是如何做到這一點的想法。