You may saw before the article Create XLSX/MHTML file from internal table in background and you may have use it without any issues before, but it seems that in newer releases of SAP (definitelly >= 7.50) this way makes corrupted XLSX files.
So I've rechecked again how it is done in ALV grid in the new NW releases and I've updated the code, so now the issue with corrupted file is solved.
The mothod is simplified as much as possible at the moment, at the end all you must pass to a method is your internal table, but you can also pass sorting, filter and layout criteria using ALV grid structures.
The definition of the method should look like this:
class-methods: create_xlsx_from_itab
importing
it_fieldcat type lvc_t_fcat optional
it_sort type lvc_t_sort optional
it_filt type lvc_t_filt optional
is_layout type lvc_s_layo optional
it_hyperlinks type lvc_t_hype optional
value(itdata) type standard table
returning value(r_xstring) type xstring.
Here goes the implementation:
method create_xlsx_from_itab.
data(lt_data) = ref #( it_data ).
if it_fieldcat is initial.
field-symbols: <tab> type standard table.
assign lt_data->* to <tab>.
try.
cl_salv_table=>factory(
exporting
list_display = abap_false
importing
r_salv_table = data(salv_table)
changing
t_table = <tab> ).
data(lt_fcat) = cl_salv_controller_metadata=>get_lvc_fieldcatalog(
r_columns = salv_table->get_columns( )
r_aggregations = salv_table->get_aggregations( ) ).
catch cx_salv_msg.
return.
endtry.
else.
lt_fcat = it_fieldcat.
endif.
cl_salv_bs_lex=>export_from_result_data_table(
exporting
is_format = if_salv_bs_lex_format=>mc_format_xlsx
ir_result_data_table = cl_salv_ex_util=>factory_result_data_table(
r_data = lt_data
s_layout = is_layout
t_fieldcatalog = lt_fcat
t_sort = it_sort
t_filter = it_filt
t_hyperlinks = it_hyperlinks )
importing
er_result_file = r_xstring ).
endmethod.
Now you can use the result XSTRING do download data to application server, local pc or use it to send via e-mail.
Cheers