CLASS zcl_itab_to_excel DEFINITION PUBLIC FINAL.
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.
Depois só chamar passando sua tabela como referência:
DATA lt_sapeiros TYPE TABLE OF MARA.
GET REFERENCE OF lt_sapeiros INTO DATA(lo_data_ref).
DATA(lv_xstring) = NEW zcl_itab_to_excel( )->itab_to_xstring( lo_data_ref ).
OPEN DATASET lv_xls_file FOR OUTPUT IN BINARY MODE.
IF sy-subrc IS INITIAL.
TRANSFER lv_xstring TO lv_xls_file.
CLOSE DATASET.
ENDIF.