Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react convert excel to json

import React, { useState } from "react";
import "./App.css";
import * as XLSX from "xlsx";

class ExcelToJson extends React.Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
    this.state = {
      file: "",
    };
  }

  handleClick(e) {
    this.refs.fileUploader.click();
  }

  filePathset(e) {
    e.stopPropagation();
    e.preventDefault();
    var file = e.target.files[0];
    console.log(file);
    this.setState({ file });

    console.log(this.state.file);
  }

  readFile() {
    var f = this.state.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);// shows that excel data is read
      console.log(this.convertToJson(data)); // shows data in json format
    };
    reader.readAsBinaryString(f);
  }

  convertToJson(csv) {
    var lines = csv.split("
");

    var result = [];

    var headers = lines[0].split(",");

    for (var i = 1; i < lines.length; i++) {
      var obj = {};
      var currentline = lines[i].split(",");

      for (var j = 0; j < headers.length; j++) {
        obj[headers[j]] = currentline[j];
      }

      result.push(obj);
    }

    //return result; //JavaScript object
    return JSON.stringify(result); //JSON
  }

  render() {
    return (
      <div>
        <input
          type="file"
          id="file"
          ref="fileUploader"
          onChange={this.filePathset.bind(this)}
        />
        <button
          onClick={() => {
            this.readFile();
          }}
        >
          Read File
        </button>
      </div>
    );
  }
}

export default ExcelToJson;
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript things to remember 
Javascript :: input from terminal node js 
Javascript :: react bootstrap table 
Javascript :: javascript how to deal with %20 in string 
Javascript :: js iife 
Javascript :: javascript wrap object in array 
Javascript :: node package.json type module 
Javascript :: javascript await return value 
Javascript :: card type through card number 
Javascript :: supertest express check response 
Javascript :: how to toggle fa fa-caret-down and fa fa-caret-up using jquery 
Javascript :: object values template literal js 
Javascript :: extended class call method from super in javascript 
Javascript :: joi regex validate 
Javascript :: videojs 100%width 
Javascript :: timestamp to unix time react 
Javascript :: returned value by findOneAndUpdate 
Javascript :: vue nuxt vuex store watch 
Javascript :: get value of textarea jquery 
Javascript :: get image src width and height 
Javascript :: react concatenate string and component 
Javascript :: jQ - on image load 
Javascript :: javascript get params from query string json object 
Javascript :: break in if statement js 
Javascript :: Split string into words, without punctuation 
Javascript :: get year javascript copyright 
Javascript :: add event listener to all a tags 
Javascript :: ./node_modules/bootstrap/dist/js/bootstrap.bundle.js 
Javascript :: sort array without mutating js 
Javascript :: import and export type in js 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =