cancel
Showing results for 
Search instead for 
Did you mean: 

How to implementate XLSX library in SAP Fiori with Javascript

UlisesCasal
Explorer
0 Kudos

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)})





            }
        }
        )
    })

 

 

 

Accepted Solutions (0)

Answers (1)

Answers (1)

Marian_Zeis
Active Contributor
0 Kudos

You can use https://spreadsheet-importer.com/ which internaly uses SheetJS(xlsx) and is easier to use.