Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Downloading a file into excel sheet

1. Create a mainview and mainwindow.


 2. Goto mainview and select context, right click on it and create->node

 3. Give name for node and select dictionary structure as VBAK and select add attribute from structure.

 4. Go to layout and in the right side select rootuielementcontainer, right click on it select insert element.

 5. Give name for it and type as table and save it.

 6. Click on the table and select create binding. Click on context then select vbak.

 7. Goto methods and doubleclick on WDDOINIT.
 Select web  dynpro code wizard(ctrl+f7).

select radiobutton read context give the node name and press enter.

*Then call method* bind_table and write code as.

 method* WDDOINIT .*
*    **DATA lo_nd_vbak TYPE REF TO if_wd_context_node.*
*    **DATA lo_el_vbak TYPE REF TO if_wd_context_element.*
*    **DATA ls_vbak TYPE wd_this->elements_vbak.*
*   navigate from <CONTEXT> to <VBAK> via lead selection
    lo_nd_vbak = wd_context->get_child_node( name = wd_this->wdctx_vbak ).

select *from vbak into corresponding fields of table ls_vbak up to 10 rows.*

*  **CALL METHOD lo_nd_vbak->bind_table*
      new_items            = ls_vbak
*      set_initial_elements = ABAP_TRUE
*      index                =

 8. Select the button and go to events and select onaction give as download.

 9. Doubleclick on download and call ctrl+f7, select radio button read context and select the node.

Then call the method get_static_attributes_table .

And function module 'SCMS_STRING_TO_XSTRING' and again call the method attach_file_to_response and write code as.

*    **DATA lo_nd_vbak TYPE REF TO if_wd_context_node.*
*    **DATA lo_el_vbak TYPE REF TO if_wd_context_element.*
*    **DATA ls_vbak TYPE wd_this->elements_vbak.*
*    **data lw_vbak like line of ls_vbak.*
*  **data str type string.*
*  **data xstr type xstring.*
*   navigate from <CONTEXT> to <VBAK> via lead selection
    lo_nd_vbak = wd_context->get_child_node( name = wd_this->wdctx_vbak ).

*    **CALL METHOD lo_nd_vbak->get_static_attributes_table*
*        from   = 1
*        to     = 2147483647
*        **table  = ls_vbak*

loopat ls_vbak into lw_vbak.*

*  **concatenate str*
*              cl_abap_char_utilities=>newline **into str*
*                **separated by cl_abap_char_utilities=>horizontal_tab.*

*    **text           = str*
*   MIMETYPE       = ' '
*   ENCODING       =
*   **BUFFER         = xstr*
*   FAILED         = 1
CALLMETHOD cl_wd_runtime_services=>attach_file_to_response*
*    i_filename      = *'Sales.xls'
    i_content       = xstr
*    i_mime_type     = *'EXCEL'
*    i_in_new_window = ABAP_FALSE
*    i_inplace       = ABAP_FALSE

 10.Save it and execute.

 Output will be as below. When we press the download button then the file will be downloaded and saved as excel file.



  1. Former Member

    Thank you Good one

  2. Works great but the only thing that has to be changed in order to get an Excelfile is the   i_mime_type     'APPLICATION/MSEXCEL'