Javascript JSON to Excel file download

前端 未结 6 2099
情书的邮戳
情书的邮戳 2020-12-31 18:14

I have Json data and i need convert json data to Excel file using javascript,

Reference URL : http://jsfiddle.net/hybrid13i/JXrwM/

i am using this code:

6条回答
  •  青春惊慌失措
    2020-12-31 18:28

    This code snippet is using node.js with the excel4node and express modules in order to convert JSON data to an Excel file and send it to the client, using Javascript.

    const xl = require('excel4node');
    const express = require('express');
    const app = express();
    
    var json = [{"Vehicle":"BMW","Date":"30, Jul 2013 09:24 AM","Location":"Hauz Khas, Enclave, New Delhi, Delhi, India","Speed":42},{"Vehicle":"Honda CBR","Date":"30, Jul 2013 12:00 AM","Location":"Military Road,  West Bengal 734013,  India","Speed":0},{"Vehicle":"Supra","Date":"30, Jul 2013 07:53 AM","Location":"Sec-45, St. Angel's School, Gurgaon, Haryana, India","Speed":58},{"Vehicle":"Land Cruiser","Date":"30, Jul 2013 09:35 AM","Location":"DLF Phase I, Marble Market, Gurgaon, Haryana, India","Speed":83},{"Vehicle":"Suzuki Swift","Date":"30, Jul 2013 12:02 AM","Location":"Behind Central Bank RO, Ram Krishna Rd by-lane, Siliguri, West Bengal, India","Speed":0},{"Vehicle":"Honda Civic","Date":"30, Jul 2013 12:00 AM","Location":"Behind Central Bank RO, Ram Krishna Rd by-lane, Siliguri, West Bengal, India","Speed":0},{"Vehicle":"Honda Accord","Date":"30, Jul 2013 11:05 AM","Location":"DLF Phase IV, Super Mart 1, Gurgaon, Haryana, India","Speed":71}]
    
    const createSheet = () => {
    
      return new Promise(resolve => {
    
    // setup workbook and sheet
    var wb = new xl.Workbook();
    
    var ws = wb.addWorksheet('Sheet');
    
    // Add a title row
    
    ws.cell(1, 1)
      .string('Vehicle')
    
    ws.cell(1, 2)
      .string('Date')
    
    ws.cell(1, 3)
      .string('Location')
    
    ws.cell(1, 4)
      .string('Speed')
    
    // add data from json
    
    for (let i = 0; i < json.length; i++) {
    
      let row = i + 2
    
      ws.cell(row, 1)
        .string(json[i].Vehicle)
    
      ws.cell(row, 2)
        .date(json[i].Date)
    
      ws.cell(row, 3)
        .string(json[i].Location)
    
      ws.cell(row, 4)
        .number(json[i].Speed)
    }
    
    resolve( wb )
    
      })
    }
    
    app.get('/excel', function (req, res) {
    
      createSheet().then( file => {
    file.write('ExcelFile.xlsx', res);
      })
    
    });
    
    app.listen(3040, function () {
      console.log('Excel app listening on port 3040');
    });
    

提交回复
热议问题