get excel file data to json

Solutions on MaxInterview for get excel file data to json by the best coders in the world

showing results for - "get excel file data to json"
Vincenzo
23 Aug 2018
1//you must have xlsx
2//npm install xlsx
3let selectedFile;
4console.log(window.XLSX);
5document.getElementById('files').addEventListener("change", (event) => {
6    selectedFile = event.target.files[0];
7})
8
9let data=[{
10    "name":"placement",
11    "data":"worksheet",
12    "abc":"efg"
13}]
14
15
16document.getElementById('files').addEventListener("change", () => {
17    XLSX.utils.json_to_sheet(data, 'Godplacement.xlsx');
18    if(selectedFile){
19        let fileReader = new FileReader();
20        fileReader.readAsBinaryString(selectedFile);
21        fileReader.onload = (event)=>{
22         let data = event.target.result;
23         let workbook = XLSX.read(data,{type:"binary"});
24         console.log(workbook);
25         workbook.SheetNames.forEach(sheet => {
26              let rowObject = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheet]);
27              console.log(rowObject);
28              document.getElementById("jsondata").innerHTML = JSON.stringify(rowObject,undefined,4)
29         });
30        }
31    }
32});