Noman Ali
Noman Ali

Reputation: 3340

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?

Upvotes: 40

Views: 130569

Answers (5)

you can use exceljs for reactjs, for example

import React from 'react';
import Excel from 'exceljs';

const Index = () => {
    const handleChange = (e) => {
        const file = e.target.files[0];
        const wb = new Excel.Workbook();
        const reader = new FileReader();

        reader.readAsArrayBuffer(file);
        reader.onload = () => {
            const buffer = reader.result;
            wb.xlsx.load(buffer).then((workbook) => {
                console.log(workbook, 'workbook instance');
                workbook.eachSheet((sheet, id) => {
                    sheet.eachRow((row, rowIndex) => {
                        console.log(row.values, rowIndex);
                    });
                });
            });
        };
    };

    return (
        <div>
            <div>Upload Excel File</div>
            <input type='file' onChange={(e) => handleChange(e)} />
        </div>
    );
};

Index.propTypes = {};
Index.defaultProps = {};

export default Index;

Upvotes: 4

Debraj Banerjee
Debraj Banerjee

Reputation: 321

One of the simplest way found here: This approach works for reading .xlsx, .txt or other formats as well.

Snippet:

import * as XLSX from 'xlsx';
...

<input
type="file"
onInput={(e) => this._handleFile(e)}
/>
...

private _handleFile = async (e: any) => {
    console.log('reading input file:');
    const file = e.target.files[0];
    const data = await file.arrayBuffer();
    const workbook = XLSX.read(data);
    const worksheet = workbook.Sheets[workbook.SheetNames[0]];
    const jsonData = XLSX.utils.sheet_to_json(worksheet, {
        header: 1,
        defval: "",
    });

    //console.log(e.target.files[0]);
    //console.log(workbook);
    console.log(jsonData);
}

Upvotes: 9

Noman Ali
Noman Ali

Reputation: 3340

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);

Upvotes: 47

Yanov
Yanov

Reputation: 673

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)
}

Upvotes: 20

rahul_sann
rahul_sann

Reputation: 314

I find xlsx to work pretty well. xlsx Package

Upvotes: 4

Related Questions