-
Notifications
You must be signed in to change notification settings - Fork 40
/
Fetch Excel Data in React js
63 lines (57 loc) · 1.97 KB
/
Fetch Excel Data in React js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React, { useState } from "react";
import { Container } from "react-bootstrap";
import *as xlsx from 'xlsx';
// npm install xlsx
function Fetchexceldata()
{
const [excelData, setExcelData]= useState([]);
const readExcel = async(e)=>{
const file= e.target.files[0];
const data= await file.arrayBuffer(file);
const excelfile= xlsx.read(data);
const excelsheet= excelfile.Sheets[excelfile.SheetNames[0]];
const exceljson= xlsx.utils.sheet_to_json(excelsheet);
//console.log(exceljson);
setExcelData(exceljson);
}
return(
<React.Fragment>
<Container className="content">
<div className="row fthight">
<div className="col-md-4 ">
<h3 className='mt-3'>Fetch Excel Data in React js</h3>
<label className="form-label">File </label>
<input type="file" className="form-control" onChange={ (e)=>readExcel(e)} />
</div>
<div className="col-md-12 mt-3">
{ excelData.length > 1 && (
<table className="table">
<thead>
<tr>
<th>Sr. No</th>
<th>Name</th>
<th>Email</th>
<th>Phone No</th>
</tr>
</thead>
<tbody>
{
excelData.map( (getdata, index)=>(
<tr key={index}>
<td>{ index+1 }</td>
<td>{ getdata.name} </td>
<td>{ getdata.email} </td>
<td>{ getdata.phoneno} </td>
</tr>
) ) }
</tbody>
</table>
)
}
</div>
</div>
</Container>
</React.Fragment>
);
}
export default Fetchexceldata;