Reading excel file in Reactjs
Asked Answered
W

5

40

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?

Wiburg answered 24/10, 2017 at 11:22 Comment(0)
W
47

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);
Wiburg answered 25/10, 2017 at 7:18 Comment(4)
but i have a doubt in this. how to restrict the brows file should be only .xlsx format. Other files should allow to uploadEgocentrism
how are you getting the file? Do you have to use a require with excel-loader?Mirnamirror
Here is the repo demo for react github.com/SheetJS/sheetjs/tree/…Extinctive
For me there is a problem that if there is empty cell there is no column key and value in data at all and I expect to be like { colName: null }Evangelina
D
20

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)
}
Desman answered 30/9, 2019 at 8:22 Comment(0)
S
8

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);
}
Sophisticated answered 18/12, 2022 at 3:40 Comment(0)
B
4

I find xlsx to work pretty well. xlsx Package

Branca answered 25/10, 2017 at 7:27 Comment(1)
Could you please answer this then> #46911579Wiburg
S
4

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;

Sollars answered 6/2, 2023 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.