In this post you will learn how to Write Excel File to Presentation Server.

Create a method to convert internal table to Excel xstring.

Class Definition and Implementation

CLASS zcl_itab_to_excel DEFINITION PUBLIC FINAL
  CREATE PUBLIC.

  PUBLIC SECTION.
    METHODS:
      itab_to_xstring 
        IMPORTING ir_data_ref       TYPE REF TO data
        RETURNING VALUE(rv_xstring) TYPE xstring.
ENDCLASS.

CLASS zcl_itab_to_excel IMPLEMENTATION.
  METHOD itab_to_xstring.

    FIELD-SYMBOLS: <fs_data> TYPE ANY TABLE.

    CLEAR rv_xstring.
    ASSIGN ir_data_ref->* TO <fs_data>.

    TRY.
        cl_salv_table=>factory(
          IMPORTING r_salv_table = DATA(lo_table)
          CHANGING  t_table      = <fs_data> ).
  
        DATA(lt_fcat) = 
          cl_salv_controller_metadata=>get_lvc_fieldcatalog(
            r_columns      = lo_table->get_columns( )
            r_aggregations = lo_table->get_aggregations( ) ).

        DATA(lo_result) =
          cl_salv_ex_util=>factory_result_data_table( 
            r_data         = ir_data_ref
            t_fieldcatalog = lt_fcat ).

        cl_salv_bs_tt_util=>if_salv_bs_tt_util~transform(
          EXPORTING
            xml_type      = if_salv_bs_xml=>c_type_xlsx
            xml_version   = cl_salv_bs_a_xml_base=>get_version( )
            r_result_data = lo_result
            xml_flavour   = if_salv_bs_c_tt=>c_tt_xml_flavour_export
            gui_type      = if_salv_bs_xml=>c_gui_type_gui
          IMPORTING
            xml           = rv_xstring ).
      CATCH cx_root.
        CLEAR rv_xstring.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.

Then, the method can be called to get the xstring and downloaded to presentation server.

TYPES: BEGIN OF xls_line,
         data(256) TYPE x,
       END OF xls_line.

DATA : lt_bin_data TYPE STANDARD TABLE OF xls_line.

"Select data from sflight
SELECT * FROM sflight INTO TABLE @DATA(lt_flights).
IF sy-subrc EQ 0.
  GET REFERENCE OF lt_flights INTO DATA(lr_data_ref).
  DATA(lv_xstring) = itab_to_excel_xstring( lr_data_ref ).

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

  cl_gui_frontend_services=>gui_download(
    EXPORTING
      filename = CONV #( iv_file )
      filetype = 'BIN'
    IMPORTING
      filelength = DATA(lv_len)
    CHANGING
      data_tab = lt_bin_data ).
ENDIF.

The code is simple, hence I have not added any explaination.

Visit ABAP Code Samples page for more code samples.


If you like the content, please subscribe…

Join 4,016 other subscribers

Discovering ABAP YouTube Channel