Created
October 4, 2019 07:04
-
-
Save fadziljusri/4108eb44df4671f12fad783b6f5d3de3 to your computer and use it in GitHub Desktop.
Excel to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<input ref="fileupload" type="file" @change="onFileChange" accept=".xlsx, .xls, .csv" /> | |
</template> | |
<script> | |
/*** | |
* Spreadsheet data toolkit https://github.com/SheetJS/js-xlsx | |
* npm i xlsx | |
*/ | |
import XLSX from "xlsx"; | |
export default { | |
name: "ExcelJson", | |
methods: { | |
onFileChange(oEvent) { | |
let oFile = oEvent.target.files[0]; | |
if (!oFile) return; | |
let sFilename = oFile.name; | |
let reader = new FileReader(); | |
let result = {}; | |
reader.onload = function(e) { | |
let data = e.target.result; | |
data = new Uint8Array(data); | |
let workbook = XLSX.read(data, { type: "array" }); | |
console.log(workbook); | |
let result = {}; | |
workbook.SheetNames.forEach(function(sheetName) { | |
let roa = XLSX.utils.sheet_to_json(workbook.Sheets[sheetName], { | |
raw: true | |
}); | |
if (roa.length) result[sheetName] = roa; | |
}); | |
// see the result, caution: it works after reader event is done. | |
console.log("result", result); | |
}; | |
reader.readAsArrayBuffer(oFile); | |
this.$refs.fileupload.value = null; | |
} | |
} | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment