Variante 1 (to_xml)
DATA: it_spfli TYPE STANDARD TABLE OF spfli.
DATA: o_salv TYPE REF TO cl_salv_table.
START-OF-SELECTION.
* Daten holen
SELECT * FROM spfli INTO TABLE @it_spfli.
* ALV-objekt erzeugen
cl_salv_table=>factory( IMPORTING
r_salv_table = o_salv
CHANGING
t_table = it_spfli ).
* itab (data) -> xstring (bytes)
DATA(lv_xml_bytes) = o_salv->to_xml( xml_type = if_salv_bs_xml=>c_type_xlsx ).
* xstring -> itab (bytes)
DATA: lv_size TYPE i.
DATA: it_raw_data TYPE xml_rawdata.
* xstring (Bytes) -> RAW (iTab)
cl_scp_change_db=>xstr_to_xtab( EXPORTING
im_xstring = lv_xml_bytes
IMPORTING
ex_size = lv_size
ex_xtab = it_raw_data ).
IF lines( it_raw_data ) > 0.
DATA: lv_action TYPE i.
DATA: lv_filename TYPE string.
DATA: lv_fullpath TYPE string.
DATA: lv_path TYPE string.
TRY.
* SaveDialog aufrufen
cl_gui_frontend_services=>file_save_dialog( EXPORTING
default_extension = 'xlsx'
default_file_name = 'export.xlsx'
file_filter = |Excel-Datei (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
prompt_on_overwrite = abap_true
CHANGING
filename = lv_filename
path = lv_path
fullpath = lv_fullpath
user_action = lv_action ).
IF lv_action EQ cl_gui_frontend_services=>action_ok.
* iTab (bytes) -> lokale Datei
cl_gui_frontend_services=>gui_download( EXPORTING
filename = lv_fullpath
filetype = 'BIN'
bin_filesize = lv_size " Size ist wichtig für das korrekte Schreiben der Excel-Datei
CHANGING
data_tab = it_raw_data ).
* lokale Datei im Excel aufrufen
* parameter muss wegen möglicher Leerzeichen im Pfad mit "" quotiert werden
cl_gui_frontend_services=>execute( application = 'excel.exe'
parameter = |"{ lv_fullpath }"| ).
ENDIF.
CATCH cx_root INTO DATA(e_txt).
MESSAGE e_txt->get_text( ) TYPE 'S'.
ENDTRY.
ENDIF.
Variante 2 (cl_salv_bs_ex_office2007)
* produziert defekte *.xlsx-Datei
DATA: it_spfli TYPE STANDARD TABLE OF spfli.
DATA: o_salv TYPE REF TO cl_salv_table.
START-OF-SELECTION.
* Daten holen
SELECT * FROM spfli INTO TABLE @it_spfli.
* ALV-objekt erzeugen
cl_salv_table=>factory( IMPORTING
r_salv_table = o_salv
CHANGING
t_table = it_spfli ).
* Version prüfen
IF cl_salv_bs_a_xml_base=>get_version( ) EQ if_salv_bs_xml=>version_25 OR
cl_salv_bs_a_xml_base=>get_version( ) EQ if_salv_bs_xml=>version_26.
* MVC-Feldkatalog erzeugen
DATA(it_fcat) = cl_salv_controller_metadata=>get_lvc_fieldcatalog( r_columns = o_salv->get_columns( )
r_aggregations = o_salv->get_aggregations( ) ).
* CL_SALV_EX_RESULT_DATA_TABLE erzeugen
DATA(o_salv_ex_res) = cl_salv_ex_util=>factory_result_data_table( r_data = REF #( it_spfli )
t_fieldcatalog = it_fcat ).
* CL_SALV_EX_RESULT_DATA_TABLE -> XML (Bytes)
DATA(o_excel2007) = NEW cl_salv_bs_ex_office2007( o_salv_ex_res ).
IF o_excel2007 IS BOUND.
DATA(lv_xml_bytes) = o_excel2007->transform( ).
* xstring -> itab (bytes)
DATA: lv_size TYPE i.
DATA: it_raw_data TYPE xml_rawdata.
* xstring (Bytes) -> RAW (iTab)
cl_scp_change_db=>xstr_to_xtab( EXPORTING
im_xstring = lv_xml_bytes
IMPORTING
ex_size = lv_size
ex_xtab = it_raw_data ).
IF lines( it_raw_data ) > 0.
DATA: lv_action TYPE i.
DATA: lv_filename TYPE string.
DATA: lv_fullpath TYPE string.
DATA: lv_path TYPE string.
TRY.
* SaveDialog aufrufen
cl_gui_frontend_services=>file_save_dialog( EXPORTING
default_extension = 'xlsx'
default_file_name = 'export.xlsx'
file_filter = |Excel-Datei (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
prompt_on_overwrite = abap_true
CHANGING
filename = lv_filename
path = lv_path
fullpath = lv_fullpath
user_action = lv_action ).
IF lv_action EQ cl_gui_frontend_services=>action_ok.
* iTab (bytes) -> lokale Datei
cl_gui_frontend_services=>gui_download( EXPORTING
filename = lv_fullpath
filetype = 'BIN'
bin_filesize = lv_size " Size ist wichtig für das korrekte Schreiben der Excel-Datei
CHANGING
data_tab = it_raw_data ).
* lokale Datei im Excel aufrufen
* parameter muss wegen möglicher Leerzeichen im Pfad mit "" quotiert werden
cl_gui_frontend_services=>execute( application = 'excel.exe'
parameter = |"{ lv_fullpath }"| ).
ENDIF.
CATCH cx_root INTO DATA(e_txt).
MESSAGE e_txt->get_text( ) TYPE 'S'.
ENDTRY.
ENDIF.
ENDIF.
ENDIF.
Variante 3 (CL_SALV_BS_TT_UTIL)
* produziert defekte *.xlsx-Datei
DATA: it_spfli TYPE STANDARD TABLE OF spfli.
DATA: o_salv TYPE REF TO cl_salv_table.
START-OF-SELECTION.
* Daten holen
SELECT * FROM spfli INTO TABLE @it_spfli.
* ALV-objekt erzeugen
cl_salv_table=>factory( IMPORTING
r_salv_table = o_salv
CHANGING
t_table = it_spfli ).
* Version prüfen
IF cl_salv_bs_a_xml_base=>get_version( ) EQ if_salv_bs_xml=>version_25 OR
cl_salv_bs_a_xml_base=>get_version( ) EQ if_salv_bs_xml=>version_26.
* MVC-Feldkatalog erzeugen
DATA(it_fcat) = cl_salv_controller_metadata=>get_lvc_fieldcatalog( r_columns = o_salv->get_columns( )
r_aggregations = o_salv->get_aggregations( ) ).
* SALV-Result-Table erzeugen
DATA(r_result_data) = cl_salv_ex_util=>factory_result_data_table( r_data = REF #( it_spfli )
t_fieldcatalog = it_fcat ).
DATA: lv_xml_bytes TYPE xstring.
* SALV-Result-Table
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 = r_result_data
xml_flavour = if_salv_bs_c_tt=>c_tt_xml_flavour_export
gui_type = if_salv_bs_xml=>c_gui_type_gui
IMPORTING
xml = lv_xml_bytes ).
* xstring -> itab (bytes)
DATA: lv_size TYPE i.
DATA: it_raw_data TYPE xml_rawdata.
* xstring (Bytes) -> RAW (iTab)
cl_scp_change_db=>xstr_to_xtab( EXPORTING
im_xstring = lv_xml_bytes
IMPORTING
ex_size = lv_size
ex_xtab = it_raw_data ).
IF lines( it_raw_data ) > 0.
DATA: lv_action TYPE i.
DATA: lv_filename TYPE string.
DATA: lv_fullpath TYPE string.
DATA: lv_path TYPE string.
TRY.
* SaveDialog aufrufen
cl_gui_frontend_services=>file_save_dialog( EXPORTING
default_extension = 'xlsx'
default_file_name = 'export.xlsx'
file_filter = |Excel-Datei (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
prompt_on_overwrite = abap_true
CHANGING
filename = lv_filename
path = lv_path
fullpath = lv_fullpath
user_action = lv_action ).
IF lv_action EQ cl_gui_frontend_services=>action_ok.
* iTab (bytes) -> lokale Datei
cl_gui_frontend_services=>gui_download( EXPORTING
filename = lv_fullpath
filetype = 'BIN'
bin_filesize = lv_size " Size ist wichtig für das korrekte Schreiben der Excel-Datei
CHANGING
data_tab = it_raw_data ).
* lokale Datei im Excel aufrufen
* parameter muss wegen möglicher Leerzeichen im Pfad mit "" quotiert werden
cl_gui_frontend_services=>execute( application = 'excel.exe'
parameter = |"{ lv_fullpath }"| ).
ENDIF.
CATCH cx_root INTO DATA(e_txt).
MESSAGE e_txt->get_text( ) TYPE 'S'.
ENDTRY.
ENDIF.
ENDIF.
Variante 4 (CL_SALV_BS_LEX)
DATA: it_spfli TYPE STANDARD TABLE OF spfli WITH DEFAULT KEY.
SELECT * FROM spfli INTO TABLE @it_spfli.
TRY.
DATA: o_salv TYPE REF TO cl_salv_table.
* SALV-Objekt erzeugen
cl_salv_table=>factory( IMPORTING
r_salv_table = o_salv
CHANGING
t_table = it_spfli ).
* Feldkatalog erstellen
DATA(o_cols) = o_salv->get_columns( ).
DATA(o_aggr) = o_salv->get_aggregations( ).
DATA(it_fcat) = cl_salv_controller_metadata=>get_lvc_fieldcatalog( r_columns = o_cols
r_aggregations = o_aggr ).
* CL_SALV_EX_RESULT_DATA_TABLE erzeugen
DATA(o_salv_ex_res) = cl_salv_ex_util=>factory_result_data_table( r_data = REF #( it_spfli )
t_fieldcatalog = it_fcat ).
DATA: lv_xml_bytes TYPE xstring.
* CL_SALV_EX_RESULT_DATA_TABLE -> XML (Bytes)
* für CSV: is_format = if_salv_bs_lex_format=>mc_format_csv
cl_salv_bs_lex=>export_from_result_data_table( EXPORTING
is_format = if_salv_bs_lex_format=>mc_format_xlsx
ir_result_data_table = o_salv_ex_res
IMPORTING
er_result_file = lv_xml_bytes ).
* wenn Byte-Daten vorhanden
IF xstrlen( lv_xml_bytes ) > 0.
DATA: lv_size TYPE i.
DATA: it_raw_data TYPE xml_rawdata.
* xstring (Bytes) -> RAW (iTab)
cl_scp_change_db=>xstr_to_xtab( EXPORTING
im_xstring = lv_xml_bytes
IMPORTING
ex_size = lv_size
ex_xtab = it_raw_data ).
IF lines( it_raw_data ) > 0.
DATA: lv_action TYPE i.
DATA: lv_filename TYPE string.
DATA: lv_fullpath TYPE string.
DATA: lv_path TYPE string.
cl_gui_frontend_services=>file_save_dialog( EXPORTING
default_extension = 'xlsx'
default_file_name = 'export.xlsx'
file_filter = |Excel-Datei (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
prompt_on_overwrite = abap_true
CHANGING
filename = lv_filename
path = lv_path
fullpath = lv_fullpath
user_action = lv_action ).
IF lv_action EQ cl_gui_frontend_services=>action_ok.
cl_gui_frontend_services=>gui_download( EXPORTING
filename = lv_fullpath
filetype = 'BIN'
bin_filesize = lv_size " Size ist wichtig für das korrekte Schreiben der Excel-Datei
CHANGING
data_tab = it_raw_data ).
ENDIF.
ENDIF.
ENDIF.
CATCH cx_root INTO DATA(e_txt).
MESSAGE e_txt->get_text( ) TYPE 'S'.
ENDTRY.
Variante 5 (EDX_UTIL)
TRY.
SELECT *
INTO TABLE @DATA(it_t001)
FROM mara
UP TO 100 ROWS.
IF sy-subrc = 0.
DATA: lv_xstr TYPE xstring.
* Creates XLS fom the ITAB
edx_util=>create_xls_from_itab( EXPORTING i_xlsx = abap_true
IMPORTING e_xstring = lv_xstr
CHANGING ct_data = it_t001 ).
IF xstrlen( lv_xstr ) > 0.
DATA: lv_action TYPE i.
DATA: lv_filename TYPE string.
DATA: lv_fullpath TYPE string.
DATA: lv_path TYPE string.
* Save-Dialog
cl_gui_frontend_services=>file_save_dialog( EXPORTING
default_file_name = 'Excel.xlsx'
default_extension = 'xlsx'
file_filter = |Excel-Datei (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
CHANGING
filename = lv_filename
path = lv_path
fullpath = lv_fullpath
user_action = lv_action ).
IF lv_action EQ cl_gui_frontend_services=>action_ok.
* XSTRING -> SOLIX (RAW)
DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( EXPORTING iv_xstring = lv_xstr ).
* Datei lokal speichern
cl_gui_frontend_services=>gui_download( EXPORTING
filename = lv_fullpath
filetype = 'BIN'
bin_filesize = xstrlen( lv_xstr )
CHANGING
data_tab = it_raw_data ).
ENDIF.
ENDIF.
ENDIF.
CATCH cx_root INTO DATA(e_txt).
WRITE: / e_txt->get_text( ).
ENDTRY.
Links