Skip to Content
Author's profile photo Rajwin Singh Sood

Custom Download To Excel Application In SAP CRM WEBUI

Title.PNG

Applies to:

SAP CRM 7.0, EHP1, EHP2 and EHP3

Summary

 

There are umpteen ways of incorporating downloading the contents in SAP CRM WEBUI framework like using transformation to convert content from ABAP to XLS and then later using the converted content from transformation to be displayed on WEBUI framework using output stream. Although this approach is appropriate in case where we need output in excel sheet in particular format like specific font, background color, calculation fields and so on. But in some cases we may need simple download functionality which will be covered here. The scenario/requirement  which I had encountered was simple download of data in the form of internal table populated from third party system using Remote function(RFC) enabled function module. I may not be able to showcase the complete scenario but will showcasing the approach to simply download the string content to excel. This particular step by step approach could be used to download any other application like PDF, MS Word and the mime type(MIME table SDOKMIME and SDOKMIME_C) supported in the system.

 

 

 

Author(s):

 

Rajwin Singh Sood

Company: Atos

Created on: 8th Dec 2015

 

 

 

 

Author Bio

My_photo.PNG

Rajwin Singh Sood is currently working as Team lead/Solution

 

Architect at Atos. He has got experience of about 10 years in SAP ABAP and SAP CRM web UI. He is also a

 

SAP CRM EHP2 Certified associated consultant. Prior to working with Atos he had worked with SAP India(SAP

 

Global Delivery), Capgemini , Accenture and Infosys. He worked in SAP CRM Web Ui in areas like BSP

 

enhancements, transaction launchers, BOL Programming, BRFPlus .

Scenario

Button “Excel Download” will be added at the search result list view on click of that excel sheet will be opened with the text “Excel generation” and “this is automated generated excel sheet”.

Coding Logic

Firstly BSP component  BT115QS_SLSQ/SlsQuotSR  is enhanced. then redefine the method DO_PREPARE_OUTPUT and add the below code to add the button:-

METHOD do_prepare_output.

  CALL METHOD super->do_prepare_output

    EXPORTING

      iv_first_time = iv_first_time.

  CONSTANTS lc_button_click TYPE string VALUE ‘OPEN_EXCEL’.

  DATA:    ls_button          TYPE crmt_thtmlb_button.

  READ TABLE gt_button TRANSPORTING NO FIELDS WITH KEY text = ‘Excel Download’. “#EC NOTEXT

  IF sy-subrc  IS NOT INITIAL.

    ls_button-type     = cl_thtmlb_util=>gc_icon_object_report.

    ls_button-text     = ‘Excel Download’.                  “#EC NOTEXT

    ls_button-on_click = lc_button_click.

    ls_button-enabled  = abap_true.

    APPEND ls_button TO gt_button.

  ENDIF.

ENDMETHOD.

Secondly add the event with name “OPEN_EXCEL” and the method EH_ONOPEN_EXCEL is created with below code added:-

METHOD eh_onopen_excel.

* Added by wizard: Handler for event ‘OPEN_EXCEL’

  DATA lv_string_xls.

  DATA: guid             TYPE        guid_32.

  DATA: lv_output        TYPE string,

        lr_response      TYPE REF TO if_http_response,

        lv_xls_len       TYPE i,

        lv_url           TYPE string,

        lv_buffer        TYPE xstring.

  CONCATENATE ‘Excel generation’

              ‘this is automated generated excel sheet’

              INTO lv_string_xls

              SEPARATED BY

              cl_abap_char_utilities=>horizontal_tab.

*****generate the URL for excelsheet for be opened in .htm page

  CREATE OBJECT lr_response TYPE cl_http_response

    EXPORTING

      add_c_msg = 1.

  lv_output = lv_string_xls.

  “Convert the string into xstring.

  CALL FUNCTION ‘SCMS_STRING_TO_XSTRING’

    EXPORTING

      text     = lv_output

      mimetype = ‘APPLICATION/MSEXCEL’

    IMPORTING

      buffer   = lv_buffer.

  “Find the length of the xls file.

  lv_xls_len = xstrlen( lv_buffer ).

  ” Set parameter

  lr_response->set_data(

    data   = lv_buffer

    length = lv_xls_len      ).

  lr_response->set_header_field(

  name  = if_http_header_fields=>content_type

  value = if_ai_attachment=>c_mimetype_excel ).

  lr_response->set_header_field(

  name  = ‘expires’

  value = ‘180’ ).

  lr_response->set_status(

    code = 200

    reason = ‘OK’ ).

  lr_response->server_cache_expire_rel(

    expires_rel = 180 ).

  CALL FUNCTION ‘GUID_CREATE’

    IMPORTING

      ev_guid_32 = guid.

  “create the url

  CONCATENATE runtime->application_url ‘/’ guid ‘.xls’

  INTO gv_url.

  “upload the content into application server cache

  cl_http_server=>server_cache_upload( url = gv_url

  response = lr_response ).

ENDMETHOD.

Where gv_url is global variable created in the Implementation Class ZL_BT115QS__SLSQUOTSR_IMPL  of type string to be used for opening the new window using java script code window.open.

and finally the code in .HTM page to open the excel application as give below:-

<%

  DATA lv_url TYPE string.

  IF controller->gv_url IS NOT INITIAL.

  lv_url = controller->gv_url.

  CLEAR controller->gv_url.

%>

<script type=”text/javascript”>

window.open( “<%= lv_url%>”,”<%= sy-uzeit%>” );

</script>

<%

  ENDIF.

%>

Application testing

Now open the WEBUI and go to the search page of quotation as per below screenshots:-

Picture1.PNG

Once the Excel download button is clicked the new window opens with the POP UP asking to save the file:-

Picture2.PNG

and the excel sheet is opened :-

pic3.PNG

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo vamshi naik
      vamshi naik

      Hi Rajwin,

      very nice post, I have a problem in displaying the window. I have done same code with the new URL created in the event of the button  & .html page but the window doesn't open in my case. I have executed all the steps as above. Please help me if i am missing anything. I have written exactly same code in .html page, it gets executed and nothing comes up.

      Author's profile photo Rajwin Singh Sood
      Rajwin Singh Sood
      Blog Post Author

      As discussed on Linkedin the issue is resolved for you.

      Author's profile photo vamshi naik
      vamshi naik

      Hi Rajwin,

      I am able to download the file now with all my required data. but the file value is coming with (GUID).xls.  Is their any way we can change the name of the file and is there any option to download as CSV file.

      I have tried giving filename in the URL part of the code then it gives dump. Please let me know your input to finish my task.