cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

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 .

TYPES:

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.

DATA:

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

CALL FUNCTION 'CRM_IC_XML_STRING2XSTRING'

EXPORTING

instring = xml_out

IMPORTING

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

EXPORTING

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.

ENDMETHOD.

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!!!!

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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.

data:

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.

Regards

Nithya

Answers (1)

Answers (1)

Former Member
0 Kudos

Thank you very much for your reply!

You solved my problem!