Reading excel file in Reactjs
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);
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)
}