Skip to Content
Author's profile photo Shanthan Reddy

Download The data from Internal Table to Excel using O Data Services (SEGW-Tcode)

This document would help developers who are new to the Odata services.


The business requirement given here is to build an OData service which can retrieve all the information from the back-end and download in our system in the form of Excel format..based on a Plant or Group..as input selection screen.


Step1.

Create a RFC function module in the backend system with below parameters as import and export.



/wp-content/uploads/2014/12/img1_601754.png


Step2.


In Source Code we have to write the below code (sample of code is providing below )


From the input parameter we need to pass the Plant.


FUNCTION Z_TEST_EXCEL.
*”———————————————————————-
*”*”Local Interface:
*”  IMPORTING
*”     VALUE(IMP_WERKS) TYPE  WERKS_D OPTIONAL
*”  EXPORTING
*”     VALUE(E_URL) TYPE  STRING
*”     VALUE(E_WERKS) TYPE  WERKS_D
*”———————————————————————-

DATA :
LV_APP_TYPE       
TYPE STRING,
LV_GUID           
TYPE GUID_32,
LO_CACHED_RESPONSE
TYPE REF TO IF_HTTP_RESPONSE,
LS_MARC_STRING    
TYPE STRING,
LS_MARC_XSTRING   
TYPE XSTRING,
LT_MARC           
TYPE STANDARD TABLE OF MARC,
LS_MARC           
LIKE LINE OF LT_MARC,
LS_FILE_NAME      
TYPE STRING.

SELECT * FROM MARC INTO TABLE LT_MARC WHERE WERKS = IMP_WERKS .

E_WERKS = IMP_WERKS .

CONCATENATE LS_MARC_STRING
‘MATERIAL’              CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
‘PLANT NAME’            CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
‘MAINTENANCE STATUS’    CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
‘PURCHASING GROUP’    CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
CL_ABAP_CHAR_UTILITIES=>NEWLINE

INTO LS_MARC_STRING.

LOOP AT LT_MARC INTO  LS_MARC .

CONCATENATE LS_MARC_STRING
LS_MARC-MATNR         CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
LS_MARC-WERKS         CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
LS_MARC-PSTAT         CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
LS_MARC-EKGRP         CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB
CL_ABAP_CHAR_UTILITIES=>NEWLINE
INTO LS_MARC_STRING.

ENDLOOP.

CALL FUNCTION ‘SCMS_STRING_TO_XSTRING’
EXPORTING
TEXT  = LS_MARC_STRING
IMPORTING
BUFFER = LS_MARC_XSTRING.

CREATE OBJECT LO_CACHED_RESPONSE
TYPE
CL_HTTP_RESPONSE
EXPORTING
ADD_C_MSG =
1.

****SET THE DATA AND THE HEADERS
LO_CACHED_RESPONSE->SET_DATA( LS_MARC_XSTRING ).
LV_APP_TYPE =
‘APPLICATION/MSEXCEL; CHARSET=UTF-8’.

LO_CACHED_RESPONSE->SET_HEADER_FIELD( NAME = IF_HTTP_HEADER_FIELDS=>CONTENT_TYPE
VALUE = LV_APP_TYPE ).
****SET THE RESPONSE STATUS
LO_CACHED_RESPONSE->SET_STATUS(
CODE = 200 REASON = ‘OK’ ).

****SET THE CACHE TIMEOUT – 60 SECONDS – WE ONLY NEED THIS IN THE CACHE
****LONG ENOUGH TO BUILD THE PAGE
LO_CACHED_RESPONSE->SERVER_CACHE_EXPIRE_REL( EXPIRES_REL =
60 ).

****CREATE A UNIQUE URL FOR THE OBJECT AND EXPORT URL

CONCATENATE ‘EXPORT_’ SY-DATUM SY-UZEIT INTO LS_FILE_NAME.
CONCATENATE  ‘/SAP/PUBLIC/’  LS_FILE_NAME ‘.’ ‘XLS’ INTO E_URL.

****CACHE THE URL
CL_HTTP_SERVER=>SERVER_CACHE_UPLOAD( URL      = E_URL
RESPONSE = LO_CACHED_RESPONSE ).

ENDFUNCTION.


Step3:

Go to SAP Netweaver Gateway Service builder (SEGW) and create a project with Entity set and Entity Types

Below is the image after creating it in SEGW


/wp-content/uploads/2014/12/img2_601755.png



Step4:

Now go to Service Implementation and create a GetEntity(Read) operation

Create Mapping to the Get Entity using Rfc Function Module (only here using for Read Operation in query for a plant-1000 )

/wp-content/uploads/2014/12/img4_601766.png

Step5:

After all the mapping is completed, click on the “Generate Runtime Object” button, for generating Runtime Aircrafts.


Also Implement Service Maintenance using Register our Service and click on maintain button to direct to the URL (using Call Browser button again in next screen)


/wp-content/uploads/2014/12/img3_601767.png

Step6:

Now we are in WEB page, here we have to pass our query READ operation in main URL as shown below for plant-1000

RESULT is : plant and URL of Excel sheet Download.

/wp-content/uploads/2014/12/img5_601768.png

Step7:

After getting result,  again we have to redirect the URL parameter in Browser level  see the below screen to download the excel..

/wp-content/uploads/2014/12/img6_601793.png

Thank you…

Assigned Tags

      12 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo sathish B
      sathish B

      Nice Post....

      Valuable Info...Keep Posting.....

      Author's profile photo Former Member
      Former Member

      k, gud post, keep it up

      Author's profile photo Shanthan Reddy
      Shanthan Reddy
      Blog Post Author

      Thank You

      Author's profile photo Former Member
      Former Member

      Nice Post Shanthan..

      Good One.. 

      Author's profile photo Former Member
      Former Member

      Good stuff Shanthan..

      Author's profile photo Former Member
      Former Member

      hi Shanthan ,

      Nice Blog , I am bit confused in the last step ..

      can you briefly explain how to redirect the url to download the excel


      thank you.

      Author's profile photo Shanthan Reddy
      Shanthan Reddy
      Blog Post Author

      Redirect means just we should pass the URL.. in new page..

      here we are getting the output of URL.. Only

      Author's profile photo Former Member
      Former Member

      Are there any specific setting to be done for downloading the Sheet ?

      I am getting the below error :-URL call was terminated because the corresponding service is not available.SDN ERROR.JPG

      Author's profile photo Sridhar Reddy
      Sridhar Reddy

      Hi Shanthan,

      Thanks for this useful info.

      I have similar requirement to download data into excel from URL.

      I have followed all steps as you mentioned in the blog and working fine in Dev system.

      Once moved to Test system, this is not working. I am not sure what would be the reason but my assumptions are as below :

      1) service path /SAP/PUBLIC/ requires authorization to create file, If yes then what authorizations require for USER.

      2) Problem while Redirecting the URL.

      Please suggest me on this.

      Thanks,

      Sridhar.

      Author's profile photo sathish B
      sathish B

      Hi Sridhar,

      when you moved to Test System, the URL will be changed, you have to mention that URL there.

      once check in Test system whether the service activated or not in T-code SICF

      if not please activate the service in Test System and try

      Best Regards,

      Sathish

      Author's profile photo Shanthan Reddy
      Shanthan Reddy
      Blog Post Author

      Hi Sridhar,

      Please check in SICF Transaction for that service..

      We have to activate the service..

      Author's profile photo Shiva Krishna
      Shiva Krishna

      Hi shantanu,

      It gives me an error when i pass the value to the Entity set saying 'Invalid mapping or no mapping exists for data' .Can you please help me how to overcome this?