forked from jayanthbabu123/excel-to-json-by-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
excel.js
30 lines (27 loc) · 950 Bytes
/
excel.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
let selectedFile;
console.log(window.XLSX);
document.getElementById('input').addEventListener("change", (event) => {
selectedFile = event.target.files[0];
})
let data=[{
"name":"jayanth",
"data":"scd",
"abc":"sdef"
}]
document.getElementById('button').addEventListener("click", () => {
XLSX.utils.json_to_sheet(data, 'out.xlsx');
if(selectedFile){
let fileReader = new FileReader();
fileReader.readAsBinaryString(selectedFile);
fileReader.onload = (event)=>{
let data = event.target.result;
let workbook = XLSX.read(data,{type:"binary"});
console.log(workbook);
workbook.SheetNames.forEach(sheet => {
let rowObject = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheet]);
console.log(rowObject);
document.getElementById("jsondata").innerHTML = JSON.stringify(rowObject,undefined,4)
});
}
}
});