Skip to Content

SAPUI5: MS Excel file upload

1.   Introduction

This document contains the functionality to upload the excel file in SAP UI5 application through Gateway. For this I have consumed the custom OData service to upload the file. Users can upload the file from the HTML5 browsers (Google Chrome, IE etc…) and see the results in the desired format (List, Table, ComboBox etc…).

2.   Prerequisite

  • Eclipse with SAP UI5 plugin (1.28 or higher).
  • Upload OData service to consume.
  • Excel sheet with relevant data to upload.

3.   SAP UI5 Control

sap.ui.unified.FileUploader is used to perform the file upload operation.

4.   XML View Code

<core:View xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc"
    xmlns="sap.m" xmlns:u="sap.ui.unified" xmlns:l="sap.ui.layout.form"
    controllerName="views.FileUpload" xmlns:html="">
    <Page title="CSV File Upload">
            <l:SimpleForm editable="true">
                    <Label text="File Name"></Label>
                        <u:FileUploader id="idfileUploader" width="50%"
                        sameFilenameAllowed="false" placeholder="Choose a CSV file"
                        <Button text="Upload" press="onUpload"></Button>
            <Table id="idTable" items="{/}">
                            <Text text="{prodno}"></Text>
                            <Text text="{product}"></Text>
                            <Text text="{productname}"></Text>
                            <Text text="{uom}"></Text>
                        <Text text="Product No"></Text>
                        <Text text="Product"></Text>
                        <Text text="Product Name"></Text>
                        <Text text="Unit of Measure"></Text>

5.   JS Controller Code

sap.ui.controller("views.FileUpload", {

onInit : function() {
this._oBusyDialog = new sap.m.BusyDialog();

onUpload : function(e) {

var t=this;
var fU = this.getView().byId("idfileUploader");
var domRef = fU.getFocusDomRef();
var file = domRef.files[0];
var dublicateValue = [];
try {
if (file) {
var that = this;;

/****************To Fetch CSRF Token*******************/

var sUrl="/sap/opu/odata/SAP/Z***SRV/";
url: sUrl,
type: "GET",
beforeSend: function(xhr)
xhr.setRequestHeader("X-CSRF-Token", "Fetch");
success: function(data, textStatus, XMLHttpRequest) {
oToken =  XMLHttpRequest.getResponseHeader('X-CSRF-Token');
oHeaders = {
"x-csrf-token" : oToken,

/****************To Fetch CSRF Token*******************/

/*******************To Upload File************************/

var oURL = "/sap/opu/odata/SAP/Z***SRV/UploadSet";
type: 'POST',
url: oURL,
headers: oHeaders,
cache: false,
contentType:  ["csv"],
processData: false,
data: file,
success: function(data){                      

var isIE = false || !!document.documentMode;
if(isIE == true){
var ev_result = data.childNodes[0].lastChild.childNodes[1].textContent;
var ev_error = data.childNodes[0].lastChild.childNodes[0].textContent;  
var ev_result = data.getElementsByTagName("entry")[0].children[6].children[1].innerHTML;
var ev_error = data.getElementsByTagName("entry")[0].children[6].children[0].innerHTML;

/******* Success  *********/
var valSuccessArray = ev_result.split("//");
var itemsArray = [];
for(var i=0;i<valSuccessArray.length;i++){
var splitVal = valSuccessArray[i].split(",");
var items = {
product : splitVal[0],
productname : splitVal[1],
uom : splitVal[2]   


function removeDuplicates(originalArray, prop) {
var newArray = [];
var lookupObject  = {};

for(var i in originalArray) {
lookupObject[originalArray[i][prop]] = originalArray[i];

for(i in lookupObject) {
return newArray;

var itemsSuccessArray = removeDuplicates(itemsArray, "product");

for(var k=0;k<itemsSuccessArray.length;k++){
var countK = k+1
var prodcnt = "0000000"+ countK;
prodcnt = prodcnt.substring(prodcnt.length-8);                  
itemsSuccessArray[k].prodno = prodcnt;

var jsonModel = new sap.ui.model.json.JSONModel(itemsSuccessArray);

/******* Error  *********/
} catch(oException) {"File upload failed:\n" + oException.message);

6.   Result Screens:

Before file upload

Screenshot of excel sheet to be uploaded

After successful file upload

You must be Logged on to comment or reply to a post.