Hi, I provide training in SAP UI5. Also feel free to checkout my new web page https://saptechblog.com
Webdynpro ABAP

Exporting content of table to Excel

In this post we will see how to export the contents of the table to the excel sheet. In the previous blog we have already

created a application to display data in the form of the tables. 

 

URL: http://www.teamabap.com/2014/05/table-ui-element.html

 

Output:

 

clip_image002

 

We will continue in the same application to export the table content to the excel sheet.

 

Step 1: Go to the layout tab of the view controller and create button UI element.

 

clip_image004

 

 

Provide the ID and type for the UI element.

 

clip_image006

 

 

Enter the text property of the button UI element as export and create an action for the button UI element.

 

clip_image007

 

 

 

Step 2: In the event handler method for the button UI element do the following code to export the content to the excel sheet.

 

First read the content of the table using the code wizard read as table operation.

 

clip_image009

 

The content of the table will be exported to the internal table lt_ekko. Now  we will move these content to the excel sheet.

To do that concatenate all the data in to a string variable seperated by a horizontal tab and conver it into xstring using function module

SCMS_STRING_TO_XSTRING.

 

Concatenate the contents of the row into an string variable.

 

clip_image011

 

Pass the string variable to the function module scms_string_to_xstring and convert the values into xstring.

 

clip_image013

 

 

There is a class called wdr_task which contains an static attribute client_window. This attribute is of type reference variable to class CL_WDR_CLIENT_WINDOW

There is an attribute in the class cl_wdr_client_window called client which is again a reference variable of type IF_WDR_CLIENT. This interface contains a method

Attach_file_to_response which will help us creating the excel sheet with the contents of the variable lv_string.

 

clip_image015

 

Note : For people who don’t understand oo abap codings can write the coding in a following way.

 

clip_image017

 

Save and activate the whole component and test the  application.

 

Coding in the method for Button UI element.

 

  DATA lo_nd_ekko TYPE REF TO if_wd_context_node.

 
DATA lt_ekko TYPE
wd_this->elements_ekko.

  lo_nd_ekko = wd_context->get_child_node( name = wd_this->wdctx_ekko ).

  lo_nd_ekko->get_static_attributes_table(
IMPORTING table
= lt_ekko ).

 
DATA ls_ekko LIKE LINE OF
lt_ekko.
 
DATA lv_text TYPE
string.

 
LOOP AT lt_ekko INTO
ls_ekko.

   
CONCATENATE
lv_text
                ls_ekko-ebeln
                ls_ekko-bukrs
                ls_ekko-bstyp
                ls_ekko-aedat
                ls_ekko-lifnr
                cl_abap_char_utilities=>newline
               
INTO
lv_text
               
SEPARATED BY
cl_abap_char_utilities=>horizontal_tab.
   
CLEAR
ls_ekko.

  ENDLOOP.

 
DATA : lv_xstring TYPE
xstring.

 
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

   
EXPORTING
     
text           = lv_text
  
IMPORTING

    
BUFFER         = lv_xstring
  
EXCEPTIONS

     FAILED         =
1
    
OTHERS         = 2
            .
 
IF sy-subrc <> 0.
 
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
        
WITH
SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.



*  WDR_TASK=>CLIENT_WINDOW->CLIENT->ATTACH_FILE_TO_RESPONSE(

*    I_FILENAME = 'table.xls'
*    I_CONTENT =  lv_xstring
*    I_MIME_TYPE = 'EXCEL' ).

data : lv_clientwindow type ref to CL_WDR_CLIENT_WINDOW.

lv_clientwindow = WDR_TASK=>CLIENT_WINDOW.


data : lv_client type ref to
IF_WDR_CLIENT.

lv_client = lv_clientwindow->client.


CALL METHOD
lv_client->attach_file_to_response
 
EXPORTING

    i_filename      =
'table.xls'
    i_content       = lv_xstring
    i_mime_type     =
'EXCEL'
.

 

 

Output:

 

clip_image019

 

Click on the export button.

 

clip_image021

 

Click on open or save button.

 

When you click on save file will be downloaded with the specified name.

clip_image023

 

Click on open to view the file.

 

clip_image025

 

 

About Arun

1 Comments:

  1. Thanks Arun for sharing the knowledge. Much appreciated :)

    ReplyDelete

Powered by Blogger.