Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
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*
*    *EXPORTING
      new_items            = ls_vbak
*      set_initial_elements = ABAP_TRUE
*      index                =
      .
endmethod*.*

 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.

 method* ONACTIONDOWNLOAD .*
*    **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*
*      EXPORTING
*        from   = 1
*        to     = 2147483647
*      *IMPORTING
*        **table  = ls_vbak*
        .

loopat ls_vbak into lw_vbak.*

*  **concatenate str*
              lw_vbak-vbeln
              lw_vbak-erdat
              lw_vbak-erzet
*              cl_abap_char_utilities=>newline **into str*
*                **separated by cl_abap_char_utilities=>horizontal_tab.*
endloop*.*

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

 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.

    

3 Comments

  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'