Skip to Content
avatar image
Former Member

Export internal table to Excel file pressing a new button created in ALV

Hello, I am trying to implement the functionality to export to excel file inside a button that i have created into my ALV. I don't want to use FileDownload UI.

The code I have set for event handler of this button is the following:

METHOD attach_files .


BEGIN OF tipo_alv_tab,

tipod TYPE objid,

descd TYPE p1000-stext,

begda TYPE begdatum,

endda TYPE enddatum,

pernr TYPE pernr_d,

nombre TYPE ad_namefir,

email TYPE ad_smtpadr,

posicion TYPE p1000-stext,

uodesc TYPE p1000-stext,

END OF tipo_alv_tab.


i_alv_tab TYPE TABLE OF tipo_alv_tab,

conv_out TYPE REF TO cl_abap_conv_out_ce,

content TYPE xstring,

lv_filename TYPE string,

xml_out TYPE string.

  • Fill values from memory

IMPORT name1 TO i_alv_tab FROM MEMORY ID 'ZCA'.

  • Build XML file with internal table information

CALL TRANSFORMATION ('ID') SOURCE tab = i_alv_tab[] RESULT XML xml_out.

  • Build XSTRING with XML



instring = xml_out


outxstring = content.

  • Format XSTRING

conv_out = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' ).

  • Convert data

conv_out->convert( EXPORTING data = xml_out IMPORTING buffer = content ).

  • Save file

CALL METHOD cl_wd_runtime_services=>attach_file_to_response


i_filename = 'Excel File.xls'

i_content = content

i_mime_type = 'application/msexcel'

i_in_new_window = i_in_new_window

i_inplace = i_inplace.


When pressing the button, the file created is without extension, and with a rare name. When trying to open the file, it seems to be corrupted.

Does anyone know what am I doing wrong???

Please, help is really really appreciated!!!!

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    Jul 04, 2007 at 12:40 PM

    Hi Jorge,

    The export data to excel functionality is available inbuilt in ALV and you dont have to write any implementation. However, if you want to create your own button for it, you can do so as below.


    lr_button type ref to cl_salv_wd_fe_button,

    lr_function type ref to cl_salv_wd_function.

    CREATE OBJECT lr_button.

    lr_button->set_text( 'Export to excel' ).

    lr_button->set_tooltip( 'Export data to excel' ).

    lr_function = l_alv_model->if_salv_wd_function_settings~create_function( id = 'EXCEL' ).

    lr_function->set_function_std( IF_SALV_WD_C_STD_FUNCTIONS=>EXPORT_EXCEL ).

    lr_function->set_editor( lr_button ).

    Now, you have created your own button, created a user defined function for the ALV and set this button as editor for the function. And using the set_function_std method, we have just mapped the functionality of your new button to the existing export to excel functionality in ALV.

    Hope this is what you are looking for.



    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 04, 2007 at 12:48 PM

    Thank you very much for your reply!

    You solved my problem! 😀

    Add comment
    10|10000 characters needed characters exceeded