Code Monkey home page Code Monkey logo

fetch-data-from-excel-file's Introduction

React Excel File Data Fetcher

This project allows you to fetch data from an Excel file and display it in a React component.

image

Getting Started

Follow these steps to set up the project:

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/your-repository.git
    
  2. Navigate to the project directory:

    cd your-repository
    
  3. Install dependencies:

    npm install
    
  4. Install the required dependencies using npm or yarn:

    npm install xlsx
    
  5. Import the necessary dependencies in your React component:

    import React, { useState } from 'react';
    import * as XLSX from 'xlsx';
  6. Set up state to store the Excel data:

    const [data, setData] = useState([]);
  7. Create an input element of type "file" to allow users to choose an Excel file.

  8. Define a function to handle file upload and process the selected file:

    const handleFileUpload = (e) => {
      const file = e.target.files[0];
      const reader = new FileReader();
      reader.onload = (event) => {
        const binaryString = event.target.result;
        const workbook = XLSX.read(binaryString, { type: 'binary' });
        const sheetName = workbook.SheetNames[0];
        const sheet = workbook.Sheets[sheetName];
        const jsonData = XLSX.utils.sheet_to_json(sheet);
        setData(jsonData);
      };
      reader.readAsBinaryString(file);
    };
  9. Check if the data is not empty and display it in a table:

    return (
      <div>
        <input type="file" onChange={handleFileUpload} />
        {data.length > 0 && (
          <table>
            <thead>
              <tr>
                {Object.keys(data[0]).map((key) => (
                  <th key={key}>{key}</th>
                ))}
              </tr>
            </thead>
            <tbody>
              {data.map((row, index) => (
                <tr key={index}>
                  {Object.values(row).map((value, index) => (
                    <td key={index}>{value}</td>
                  ))}
                </tr>
              ))}
            </tbody>
          </table>
        )}
      </div>
    );

Now, when you upload an Excel file, the data will be displayed in a table.

fetch-data-from-excel-file's People

Contributors

lexzer42 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.