Excel File Upload in SAPUI5(Master data upload using SAPUI5): Below is the approach that I tried and successfully able to upload File to back-end ECC server and upload data to database.

 

Frontend (UI part)

 

In XML view

<file:FileUploader id=“fileupload” name=“File Uploader”

              useMultipart=“false” sendXHR=“true” />

<Button id=“btnUpload” press=“uploadFile” text=“Upload”></Button>

 

In Controller.js

uploadFile : function(){

var file = jQuery.sap.domById(“__xmlview2–fileupload-fu”).files[0];

                try {

                  if (file) {

                    this._bUploading = true;

                    var that = this;

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

                      var a = “/Yourservice URL or Metadata URL “;

                      var f = {

                        headers : {

                          “X-Requested-With” : “XMLHttpRequest”,

                          “Content-Type” : “application/atom+xml”,

                          DataServiceVersion : “2.0”,

                          “X-CSRF-Token” : “Fetch”

                        },

                        requestUri : a,

                        method : “GET”

                        };

                      var oHeaders;

                      var sUrl=oDataModel.sServiceUrl+“/Your Entity Set “;

                      var oModel = new sap.ui.model.odata.ODataModel(sUrl, true);                     

                      sap.ui.getCore().setModel(oModel);

                        OData.request(f, function(data, oSuccess) {

                          oToken = oSuccess.headers[‘x-csrf-token’];

                               oHeaders = {

                                            “x-csrf-token” : oToken,

                                            “slug” : “QF”,

                                     };

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

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

                   var oURL = oDataModel.sServiceUrl+“/Entity Set”;

                  jQuery.ajax({

                    type: ‘POST’,

                    url: oURL,

                    headers: oHeaders,

                    cache: false,

                    contentType: file.type,

                    processData: false,

                    data: file,

                    success: function(data){                      

                       var rec=data.getElementsByTagName(“entry”)[0].children[5].getAttribute(“src”);

                       sap.m.MessageToast.show(“File Uploaded Successfully”+rec);

                    },

                    error:function(data){

                      sap.m.MessageToast.show(“File Uploaded Successfully”);

                    }

                  });

                  });

                }

                } catch(oException) {

              jQuery.sap.log.error(“File upload failed:
+ oException.message);

                }

}

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

Note

For this blog the uploaded file excel file should be saved as .csv comma delimited format. If you upload (.XLSX, .XLS) format this code will not work for you.

In Backend ABAP System

Entity

Your entity should have this fields as mentioned above.

 

Implement Create_Stream Method

 

You need to implement /IWBEP/IF_MGW_APPL_SRV_RUNTIME~CREATE_STREAM method of DPC_EXT class

 

DATA lv_output TYPE string,
lv_out_tab
TYPE TABLE OF string,
ls_output
TYPE string.

 

DATA: gv_spl TYPE c VALUE cl_abap_char_utilities=>cr_lf.

 

DATA: lv_converter TYPE REF TO cl_abap_conv_in_ce,
lv_xstring  
TYPE xstring.

 

 

CASE iv_entity_name.
WHEN ‘YourEntity’.

lv_xstring = is_media_resourcevalue.

lv_converter = cl_abap_conv_in_ce=>create( input       = lv_xstring
encoding    = ‘UTF-8’
replacement
= ‘?’
ignore_cerr
= abap_true ).

TRY.
CALL METHOD lv_converter->read( IMPORTING data = lv_output ).
CATCH cx_sy_conversion_codepage.
*– Should ignore errors in code conversions
CATCH cx_sy_codepage_converter_init.
*– Should ignore errors in code conversions
CATCH cx_parameter_invalid_type.
CATCH cx_parameter_invalid_range.
ENDTRY.

REPLACE gv_spl IN lv_output WITH .
SPLIT lv_output AT gv_spl INTO TABLE lv_out_tab.

 

LOOP AT lv_out_tab INTO ls_output.


REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>cr_lf(1) IN ls_output WITH .
SPLIT ls_output AT ‘,’ INTO lv_marmatnr ls_char lv_marernam ls_char1 lv_maraenam lv_marvpsta lv_marpstat lv_marmtart lv_marmatkl lv_marmeins.

lv_marmatnr(1) = .
CONDENSE lv_marmatnr.


IF ls_char IS NOT INITIAL.
CALL FUNCTION ‘CONVERT_DATE_TO_INTERNAL’
EXPORTING
date_external                 
= ls_char
*             ACCEPT_INITIAL_DATE            =
IMPORTING
date_internal                 
= lv_marersda
*           EXCEPTIONS
*             DATE_EXTERNAL_IS_INVALID       = 1
*             OTHERS                         = 2
.
IF sysubrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDIF.

IF ls_char1 IS NOT INITIAL.
CALL FUNCTION ‘CONVERT_DATE_TO_INTERNAL’
EXPORTING
date_external                 
= ls_char1
*             ACCEPT_INITIAL_DATE            =
IMPORTING
date_internal                 
= lv_marlaeda
*           EXCEPTIONS
*             DATE_EXTERNAL_IS_INVALID       = 1
*             OTHERS                         = 2
.
IF sysubrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDIF.

IF lv_marmatnr IS NOT INITIAL.
APPEND lv_mar TO it_mara.
ENDIF.
CLEAR lv_mar.

ENDLOOP.

MODIFY zar_mara FROM TABLE it_mara.

ENDCASE.

New NetWeaver Information at SAP.com

Very Helpfull

 

 

User Rating: Be the first one !