Skip to Content
Technical Articles
Author's profile photo Yogendra Kotwal

Download ALV Report Output as Excel

Introduction:

 

Hi SAP Community

This Article explains code about how to download Report output in Excel through Program directly.
In case ALV report Display is not required only Excel download is required.

Excel Output will be similar as we download through
List >> Export >> Spreadsheet options
Shown as below.

 

After Report Execution File gets Downloaded to given Path as shown below.

 

Excel File Output:

 

You can try below Program to understand more:

*&---------------------------------------------------------------------*
*& Report ZALV_TO_EXCEL_DOWNLOAD
*&---------------------------------------------------------------------*
*& Download ALV Report Output as Excel file
*&---------------------------------------------------------------------*
REPORT zalv_to_excel_download.

*&---------------------------------------------------------------------*
*& Selection Screen
*&---------------------------------------------------------------------*
* Parameter for file path name input
PARAMETERS: p_fname TYPE ibipparms-path OBLIGATORY.

*&---------------------------------------------------------------------*
*& AT SELECTION-SCREEN ON VALUE-REQUEST
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

* Filemanager support to locate file in a direct
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
      field_name    = 'P_FNAME'
    IMPORTING
      file_name     = p_fname.

*&---------------------------------------------------------------------*
*& START-OF-SELECTION.
*&---------------------------------------------------------------------*
START-OF-SELECTION.

  DATA: lt_bintab   TYPE STANDARD TABLE OF solix,
        lv_size     TYPE i,
        lv_filename TYPE string.

  SELECT * FROM mara
    INTO TABLE @DATA(lt_mara)
    UP TO 5 ROWS.

  IF sy-subrc EQ 0.

* Get New Instance for ALV Table Object
    cl_salv_table=>factory(
      IMPORTING
        r_salv_table   = DATA(lo_alv)
      CHANGING
        t_table        = lt_mara ).

* Convert ALV Table Object to XML
    DATA(lv_xml) = lo_alv->to_xml( xml_type = '02' ).

* Convert XTRING to Binary
    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        buffer        = lv_xml
      IMPORTING
        output_length = lv_size
      TABLES
        binary_tab    = lt_bintab.

    lv_filename = p_fname.

* Download File
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        bin_filesize            = lv_size
        filename                = lv_filename
        filetype                = 'BIN'
      TABLES
        data_tab                = lt_bintab
      EXCEPTIONS
        file_write_error        = 1
        no_batch                = 2
        gui_refuse_filetransfer = 3
        invalid_type            = 4
        no_authority            = 5
        unknown_error           = 6
        header_not_allowed      = 7
        separator_not_allowed   = 8
        filesize_not_allowed    = 9
        header_too_long         = 10
        dp_error_create         = 11
        dp_error_send           = 12
        dp_error_write          = 13
        unknown_dp_error        = 14
        access_denied           = 15
        dp_out_of_memory        = 16
        disk_full               = 17
        dp_timeout              = 18
        file_not_found          = 19
        dataprovider_exception  = 20
        control_flush_error     = 21
        OTHERS                  = 22.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

  ENDIF.

Conclusion:

Thank you so much for reading.
I believe this is helpful.
Please share your reviews and feedback to improve the blog …

Thanks & Regards
Yogendra Kotwal

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Michael Keller
      Michael Keller

      Maybe abap2xlsx is interesting in this context 🙂

      Author's profile photo Yogendra Kotwal
      Yogendra Kotwal
      Blog Post Author

      Thank you very much for Suggestions.

      Author's profile photo Matthew Billingham
      Matthew Billingham

      Ah, the "how to download to Excel" blog for September.

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      Dude, thanks for sharing and all but seriously,it's like 1990s called, wanted their ABAP code back. F4_FILENAME, GUI_DOWNLOAD have been replaced by CL_GUI_FRONTEND_SERVICES  over a decade ago. Clean ABAP also exists now, as well as ABAP2XLSX that Michael mentioned, which removes the need to write this code in the first place.

      And, as Matthew's comment alludes, these types of posts appear on SAP Community monthly and have become sort of a running joke. Please consider searching for the existing content.

      Thanks!

      Author's profile photo Yogendra Kotwal
      Yogendra Kotwal
      Blog Post Author

      Thank you very much for Suggestions.
      I will surely try that.