Convert an Excel .xlsx file to Json using JavaScript.
excel_to_json.htm
<script src="xlsx.full.min.js"></script>
<script>
// Adaptado por kio4.com de: https://blog.bitsrc.io/csv-excel-to-json-in-javascript-70e61a1dc32d
my_file = AppInventor.getWebViewString(); // INPUT
// read Excel file and convert to json format using fetch
fetch(my_file).then(function (res) {
/* get the data as a Blob */
if (!res.ok) throw new Error("fetch failed");
return res.arrayBuffer();
})
.then(function (ab) {
/* parse the data when it is received */
var data = new Uint8Array(ab);
var workbook = XLSX.read(data, {
type: "array"
});
/* *****************************************************************
* DO SOMETHING WITH workbook: Converting Excel value to Json *
********************************************************************/
var first_sheet_name = workbook.SheetNames[0];
/* Get worksheet */
var worksheet = workbook.Sheets[first_sheet_name];
var _JsonData = XLSX.utils.sheet_to_json(worksheet, {raw: true});
/************************ End of conversion ************************/
// AppInventor.setWebViewString(JSON.stringify(_JsonData)); // No Pretty
AppInventor.setWebViewString(JSON.stringify(_JsonData, undefined, 2)); // OUTPUT
});
</script>
p70B_excel_javascript.aia (333.9 KB)
-
Adapted from:
https://blog.bitsrc.io/csv-excel-to-json-in-javascript-70e61a1dc32d -
File Excel sample from:
https://file-examples.com/index.php/sample-documents-download/sample-xls-download/ -
External xlsx.full.min.js file:
<script src="https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js"></script>