on 2024 Feb 14 8:49 PM
Hi everyone! I am developing a program in Fiori that needs to read from an Excel file that the user uploads. The problem arises when I try to use the XLSX library (which I have in the libs folder along with jszip) because when the code is executed, it returns “read is undefined”. Below is my code. Thank you very much, greetings!!!. My controller:
sap.ui.define([
"sap/ui/core/mvc/Controller",
"../libs/xlsx"
],
/**
* @Param {typeof sap.ui.core.mvc.Controller} Controller
*/
function (Controller, JSZIP, XLSX) {
"use strict";
return Controller.extend("ejercicio1.controller.Tabla", {
onInit: function () {
var oModel = new sap.ui.model.json.JSONModel();
this.getView().setModel(oModel);
},
onSelect: function (oEvent) {
var bSelected = oEvent.getParameter("selected"),
sText = oEvent.getSource().getText(),
oTable = this.byId("idProductsTable"),
aSticky = oTable.getSticky() || [];
if (bSelected) {
aSticky.push(sText);
} else if (aSticky.length) {
var iElementIndex = aSticky.indexOf(sText);
aSticky.splice(iElementIndex, 1);
}
oTable.setSticky(aSticky);
},
onFileChange: function (oEvent) {
var file = oEvent.getParameters("files").files[0];
var excelToJson = (excelFile) => {
// this function expects file
if (excelFile === null) {
return [];
}
return new Promise((resolve) => {
var reader = new FileReader();
reader.readAsArrayBuffer(excelFile);
reader.onload = (e) => {
var arrayBuffer = e.target.result;
// Parse arrayBuffer to binary string
var data = new Uint8Array(arrayBuffer);
// const binaryString = data.reduce((acc, byte) => acc + String.fromCharCode(byte), '');
// Use TextDecoder to convert binary string to proper text
var text = new TextDecoder().decode(new Uint8Array(data));
// parse excel to json
var workbook = XLSX.read(text, { type: 'binary' });
var worksheetName = workbook.SheetNames[0];
var worksheet = workbook.Sheets[worksheetName];
var jsonData = XLSX.utils.sheet_to_json(worksheet);
console.log(jsonData);
resolve(jsonData);
};
});
};
excelToJson(file).then((rows) => {console.log(rows)})
}
}
)
})
Request clarification before answering.
You can use https://spreadsheet-importer.com/ which internaly uses SheetJS(xlsx) and is easier to use.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
11 | |
9 | |
8 | |
6 | |
6 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.