0

I am new to react and need help with one of the problem. My requirement is to get specific columns data from an excel sheet and display it on the browser. For that, I am using the react-excel-renderer library. The library is here

I have installed the library.

I have installed the library

And here is my component:

import React, { Component } from "react";
import {OutTable, ExcelRenderer} from 'react-excel-renderer';

class ConvertToJson extends Component {

fileHandler = (event) => {
    let fileObj = event.target.files[0];

    //just pass the fileObj as parameter
    ExcelRenderer(fileObj, (err, resp) => {
      if(err){
        console.log(err);            
      }
      else{
        this.setState({
          cols: resp.cols,
          rows: resp.rows
        });
      }
    });               

  }

render() {
  return (
        <div>
            <input type="file" onChange={this.fileHandler.bind(this)} style={{"padding":"10px"}} />
    
            <OutTable data={this.state.rows} columns={this.state.cols} tableClassName="ExcelTable2007" tableHeaderRowClass="heading" />

            </div>
    );
    }
}

export default ConvertToJson;

I am getting the below error: enter image description here

Please help. Thanks

1 Answer 1

1

The problem is that you did not initialized state in your component. Put this line before fileHandler definition.

this.state = {
    rows: [],
    cols: []
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @sirius . Many many thanks. It works for me now. Just a quick question. How can I pick only specific fields from the excel sheet? based on the column headers ?
If you what you really want is to display specific fields, then you can use custom renderers. github.com/nadbm/react-datasheet#custom-renderers
Thanks Sirius . Really appreciated your help. I will try this custom-renderers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.