Skip to content
Skip to content
Menu
SAP ABAP DWIMAN
  • About
SAP ABAP DWIMAN

SAP ABAP – Download ALV to Excel with Total and Subtotal

By juananda.satria on August 1, 2024August 1, 2024

Bismillah..

DATA: t_data TYPE TABLE OF sflight.

START-OF-SELECTION.

  DATA: lo_alv TYPE REF TO cl_salv_table.
  DATA: lt_solix    TYPE TABLE OF solix,
        lv_filename TYPE string.
  DATA:
    lo_columns TYPE REF TO cl_salv_columns_table,
    lo_column  TYPE REF TO cl_salv_column.

  SELECT *
    FROM sflight
    INTO TABLE t_data.


  cl_salv_table=>factory(
    IMPORTING
      r_salv_table   = lo_alv    " Basis Class Simple ALV Tables
    CHANGING
      t_table        = t_data
  ).

  DATA(lo_sorts) = lo_alv->get_sorts( ).
  DATA(lo_sort) = lo_sorts->add_sort( columnname = 'PLANETYPE' subtotal = abap_true ).


  DATA(lo_aggrs) = lo_alv->get_aggregations( ).
  DATA(lo_aggr) = lo_aggrs->add_aggregation( 'PAYMENTSUM' ).

  DATA(lv_xstring) =   lo_alv->to_xml( if_salv_bs_xml=>c_type_xlsx ).

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    EXPORTING
      buffer     = lv_xstring
    TABLES
      binary_tab = lt_solix.

  lv_filename = 'C:\test_excel\file.xlsx'.

  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename                = lv_filename
      filetype                = 'BIN'
    TABLES
      data_tab                = lt_solix
    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.
    " Handle the error accordingly
    WRITE: 'Error downloading file.'.
  ELSE.
    WRITE: 'File downloaded successfully.'.
  ENDIF.

Post navigation

SAP ABAP – BDC Template
SAP ABAP – Workflow Level with Table Function

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • SAP ABAP – Generate Fiori URL
  • SAP ABAP – Workflow Agents CDS
  • SAP ABAP – Workflow Level with Table Function
  • SAP ABAP – Download ALV to Excel with Total and Subtotal
  • SAP ABAP – BDC Template

Recent Comments

  1. SAP ABAP – Simple Interface FTP Inbound (SAP Consume File From FTP) – SAP ABAP DWIMAN on SAP ABAP – String Encode & Decode BASE64
  2. Upload file – SAP ABAP DWIMAN on F4 Search Help File

Archives

  • May 2025
  • August 2024
  • June 2024
  • May 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • May 2023
  • April 2023

Categories

  • Uncategorized
©2026 SAP ABAP DWIMAN | WordPress Theme by SuperbThemes.com