Search code examples
jsonnode.jsexcelfile-conversion

Parse and convert xls file (received from GET request to URL) to JSON without writing to disk


The title says everything.

I want to get an xls file from a third party server. (said service keeps fueling records, and they do not expose any kind of api, only the excel file). Then parse that file with a library like node-excel-to-json, and convert it into JSON format I can use to import the data in mongo.

I want to manipulate the file in-memory, without writing it to disk. So, say I am getting the file with this code,

parseFuelingReport() {

    let http = require('http');
    let fs = require('fs');
    // let excel2Json = require('node-excel-to-json');

    let file = fs.createWriteStream("document.xls");
    let request = http.get("http://www.everydayexcel.com/files/Excel_Test_Basic_1_cumulative_sum.xls", function (response) {

    });


},

I want to load the response in memory and parse it with something like

        excel2Json(/* this is supposed to be the path to the xls file */, {
            'convert_all_sheet': false,
            'return_type': 'File',
            'sheetName': 'survey'
        }, function (err, output) {
            console.log('err, res', err, output);
        });

Solution

  • I assume you are using https://github.com/kashifeqbal/node-excel-to-json, which is available as node package.

    If you take a look at this line, you can see, two things:

    • It calls XLSX.readFile(filePath);, what will load a file from disk. Hard to call with an in-memory object in.
    • Internally it uses a XLSX package, most likely this one: https://www.npmjs.com/package/xlsx

    The XLSX API seems not as convenient as the excel2Json, but it provides a read() function which takes a JavaScript object:

    /* Call XLSX */
    var workbook = XLSX.read(bstr, {type:"binary"});
    

    Hope this helps