Skip to Content

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…

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