Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading excel file in Reactjs

I am trying and banging my head already while trying to read excel file in Reactjs.

I have tried multiple libraries out there like, Sheetjs , excel-parser, exceljs and so on (like 8-9) libraries.

I am getting weird and different errors in every library.

For example i am using excel-parser and getting following error

Module not found: 'child_process'

That is because it is a node module and won't work in browser.

Anyone know some good and easy library that can work with reactjs in browser?

like image 643
Noman Ali Avatar asked Oct 24 '17 11:10

Noman Ali


People also ask

How do you read excel using JavaScript?

To get more information about the data stored inside the excelData variable related to the excel file, you can print the variable using the console. log() function. Copy const XLSX = require('xlsx') ; const parseExcel = (filename) => { const excelData = XLSX. readFile(filename); return Object.

How do I read an excel file in node JS?

Explanation: First, the npm module is included in the read. js file and then the excel file is read into a workbook i.e constant file in the above program. A for loop is run until the end of the excel file starting from the first page.


3 Answers

I have successfully read excel file using Sheetjs's npm version xlsx.

Here is code:

import * as XLSX from 'xlsx';
//f = file
var name = f.name;
const reader = new FileReader();
reader.onload = (evt) => { // evt = on_file_select event
    /* Parse data */
    const bstr = evt.target.result;
    const wb = XLSX.read(bstr, {type:'binary'});
    /* Get first worksheet */
    const wsname = wb.SheetNames[0];
    const ws = wb.Sheets[wsname];
    /* Convert array of arrays */
    const data = XLSX.utils.sheet_to_csv(ws, {header:1});
    /* Update state */
    console.log("Data>>>"+data);
};
reader.readAsBinaryString(f);
like image 54
Noman Ali Avatar answered Oct 20 '22 23:10

Noman Ali


Noman Ali! Thank you.
I used, this code

const handleUpload = (e) => {
    e.preventDefault();

    var files = e.target.files, f = files[0];
    var reader = new FileReader();
    reader.onload = function (e) {
        var data = e.target.result;
        let readedData = XLSX.read(data, {type: 'binary'});
        const wsname = readedData.SheetNames[0];
        const ws = readedData.Sheets[wsname];

        /* Convert array to json*/
        const dataParse = XLSX.utils.sheet_to_json(ws, {header:1});
        setFileUploaded(dataParse);
    };
    reader.readAsBinaryString(f)
}
like image 36
Yanov Avatar answered Oct 21 '22 01:10

Yanov


I find xlsx to work pretty well. xlsx Package

like image 3
rahul_sann Avatar answered Oct 21 '22 01:10

rahul_sann