Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

This document explains how to download contents to .xlsx file in ABAP Web Dynpro. It explains downloading the file contents into .xlsx file by code snippets. Following are the steps explaining the procedure:-

  1. Define a Web Dynpro component with name for example ‘ZZDEV_WD_TEST’.
  2. Define a Button UI element in the main view with name ‘Download File’.
  3. Create a context node having attributes as structure of download file. Here I am taking structure ZSTRUCT_DOWNLOAD_XLSX for example. Users can define this node using their download file structure.

   4.  Bind the contents to be downloaded to the context node defined in step3.

   5.  On action of button UI element defined in step 2, read this context node having file contents to be downloaded into an internal table. Follow below                       mentioned code to download this contents to .xlsx file.

* Internal tables declaration

  DATA:

        lt_file_download                                    TYPE wd_this->elements_file_download,

        ls_file_download                                   TYPE wd_this->element_file_download,

        lt_column                                             TYPE if_fdt_doc_spreadsheet=>t_column.

* Structures declaration

  DATA:

        ls_column                                             TYPE LINE OF if_fdt_doc_spreadsheet=>t_column.

* Local variables declaration

  DATA:

        lv_proceed                                            TYPE boolean VALUE abap_true,

        lv_xstring                                              TYPE xstring.

* References declaration

  DATA:

        lo_nd_file_download                              TYPE REF TO if_wd_context_node,

        lref_string                                             TYPE REF TO cl_abap_datadescr,

        lref_data                                               TYPE REF TO data.

* Field symbols declaration

  FIELD-SYMBOLS:

        <fs_contents>                                       TYPE STANDARD TABLE,

        <fs_header>                                          TYPE any,

        <fs_comp>                                 TYPE any.

* Create data object

  CREATE DATA lref_data TYPE TABLE OF zstruct_download_xlsx.

* navigate from <CONTEXT> to <FILE_DOWNLOAD> via lead selection

  lo_nd_file_download = wd_context->get_child_node( name = wd_this->wdctx_file_download ).

lo_nd_file_download->get_static_attributes_table( IMPORTING table = lt_file_download ).

* Assign download contents to field symbol

  ASSIGN lref_data->* TO <fs_contents>.

  ASSIGN ls_file_download TO <fs_header>.

* Pass contents to field symbol

  <fs_contents> = lt_file_download.

* Read first row which is header

  READ TABLE <fs_contents> INTO <fs_header> INDEX 1.

  DELETE <fs_contents> INDEX 1.

* Display error message if file has no records

  IF <fs_contents> IS INITIAL.

*<< Display error message >>

*   Return without furthur processing when error encountered

    RETURN.

  ENDIF.

* Loop to prepare column header text table

  WHILE lv_proceed = abap_true.

*   Read columnwise entries

    ASSIGN COMPONENT sy-index OF STRUCTURE <fs_header> TO <fs_comp>.

*   Check if the field is the last column

    IF <fs_comp> IS NOT ASSIGNED.

*     Set loop flag

      lv_proceed = abap_false.

*     Exit the loop when a row ends

      EXIT.

    ELSE.

*     Read reference of column field name text

      lref_string ?= cl_abap_datadescr=>describe_by_data( <fs_comp> ).

*     Prepare structure for header text

      ls_column-id           = sy-index.

      ls_column-name         = <fs_comp>.

      ls_column-display_name = <fs_comp>.

      ls_column-is_result    = abap_true.

      ls_column-type         = lref_string.

*     Append structure prepared to internal table

      APPEND ls_column TO lt_column.

    ENDIF.

*   Unassign field symbol

    UNASSIGN <fs_comp>.

  ENDWHILE.

* Call class method to convert data to be downloaded to .xlsx compatible xstring

cl_fdt_xl_spreadsheet=>if_fdt_doc_spreadsheet~create_document(

      EXPORTING

        itab         = lref_data

        iv_call_type = 1

        columns      = lt_column

      RECEIVING

        xdocument    = lv_xstring ).

* Call class method to download contents in .xlsx format

cl_wd_runtime_services=>attach_file_to_response(

    EXPORTING i_filename      = 'errors.xlsx'

              i_content       = lv_xstring

              i_mime_type     = 'APPLICATION/XLSX'

              i_in_new_window = abap_false ).

   6. After executing this code a pop-up windows dialog box appears asking to open or save the .xlsx file. Click on the button as required.

12 Comments
Labels in this area