Skip to Content
Author's profile photo Former Member

Download/Write contents in .xlsx file in SAP Web Dynpro

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.

Untitled.jpg   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


        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


        ls_column                                             TYPE LINE OF if_fdt_doc_spreadsheet=>t_column.

* Local variables declaration


        lv_proceed                                            TYPE boolean VALUE abap_true,

        lv_xstring                                              TYPE xstring.

* References declaration


        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


        <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



* 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



*     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.


*   Unassign field symbol

    UNASSIGN <fs_comp>.


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



        itab         = lref_data

        iv_call_type = 1

        columns      = lt_column


        xdocument    = lv_xstring ).

* Call class method to download contents in .xlsx format


    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.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi Devesh,

        Does it support multisheet XLSX download and upload, I checked API most the useful methods are private, can you post the solution for multisheet XLSX.



      Author's profile photo Rama Krishna Kilari
      Rama Krishna Kilari

      hi devesh ,

      i tried the above code but headings are not to add them please specify.

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hello Rama Krishna,

      Please put a debugger at

      ASSIGN ls_file_download TO <fs_header>.

      and look if there are any values assigned to <fs_header>.

      If not, make a SE11 structure for ls_file_download and specify field labels.

      Please let me know if the problem persists.

      Thanks and Regards,

      Devesh Singh

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hello Anil....sorry for the late reply.

      I will work on it.

      Author's profile photo Abhilash Pradhan
      Abhilash Pradhan

      Hi Devesh,

      There are some extra sheets being displayed while creating using this way. Is there any way to avoid this ?

      Also we can use the below code to create the header.

        lo_strc ?= cl_abap_structdescr=>describe_by_name( p_name = 'ZABHI_HEADER_FIELDS' ).

        LOOP AT lo_strc->components

          ASSIGNING <l_components>.

          APPEND INITIAL LINE TO lt_column ASSIGNING <l_column>.

          IF  <l_column> IS ASSIGNED..

            MOVE: sy-tabix            TO <l_column>-id,

                  <l_components>-name TO <l_column>-name,

                  <l_components>-name TO <l_column>-display_name,

                   abap_true          TO <l_column>-is_result.

            CLEAR lo_datadescr.

            ASSIGN COMPONENT <l_column>-name

              OF STRUCTURE <l_header> TO <l_comp>.

            lo_datadescr ?= cl_abap_datadescr=>describe_by_data( <l_comp> ).

            MOVE: lo_datadescr TO <l_column>-type.



      Author's profile photo Matias Zopatti
      Matias Zopatti

      Hi, Did you find a way to avoid those extra sheets?


      Author's profile photo Srini vas
      Srini vas

      Hello Experts,

      I copied the code and its working fine, but some extra unwanted sheets are creating (for example, Decision table, Column Details....), pl. let us know is there any way to avoid creating of these unwanted sheets, we want just first sheet

      Thank you

      Author's profile photo Matias Zopatti
      Matias Zopatti

      Hi, Did you find a way to avoid those extra sheets?


      Author's profile photo Former Member
      Former Member

      hello Expert,

      would u tell me why this is not supported for  EXCEL 2003 with Chinese words. This is SAP limit or is there something I missed.

      Thanks a lot!

      Author's profile photo Lavanya A
      Lavanya A

      Hello experts,

      I am using a Simple Transformation to get a formatted xml data. How can I use that xml data to download into XLSX file??



      Author's profile photo Rohith Brungi
      Rohith Brungi

      Hi Guys,

      Thanks for the valuable document, I have tried the same as mentioned in document.

      It is working fine for small amount of records but I am getting time out error for records around 11000.

      Please advice any alternative solution.

      Author's profile photo Felipe Zanettini
      Felipe Zanettini

      Hi Devesh,

      Thank you for the great document.

      Do you have any recommendations on a feasible maximum number of records that one internal table could have in order to avoid processing time outs due to the converting of the data to xstring?