Xiang Liu
Xiang Liu

Reputation: 51

Read internal excel file using React

I have an excel file stored in src folder. I want to use react to read the excel file directly instead of uploading a file using input field and show the object array to the console, does anyone know how to do it? Any reference link about that? I saw a lot about uploading an excel file and output the data, but I don't want to upload the file. I want to import the file directly or fetch data from the file. Let me know if you know how to do in react. Thanks.

Upvotes: 2

Views: 1607

Answers (2)

manimk
manimk

Reputation: 54

  fetch(excelFile).then((res) => res.arrayBuffer())
       .then((ab) => {
           const wb        = XLSX.read(ab, { type: "array" });
           const sheetname = wb.SheetNames[0]
           const ws        = wb.Sheets[sheetname]
           const json      = XLSX.utils.sheet_to_json(ws,{header: 1,defval:''})
           console.log(json)

Here excelFile is file location, you may include your excel file location, Then json contain excel data.

Thanks.

Upvotes: 0

Aditya
Aditya

Reputation: 127

You could use FileReader API. Here's a link to similar question. You could parse/read the excel file using this package.

A little suggestion, would be to use .csv or .json files instead of .xlsx files, if you are only dealing in data.

Upvotes: 1

Related Questions