5 1 1 1 1 1 Rating 100% (1 Vote)

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:

                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(itdatatype standard table

      returning value(r_xstringtype xstring.

Here goes the implementation:


  method create_xlsx_from_itab.
    data(lt_dataref #it_data ).

    if it_fieldcat is initial.
      field-symbols<tab> type standard table.
      assign lt_data->to <tab>.
            list_display abap_false
            r_salv_table data(salv_table)
            t_table      <tab> ).

                                   r_columns      salv_table->get_columns)
                                   r_aggregations salv_table->get_aggregations).
        catch cx_salv_msg.

      lt_fcat it_fieldcat.

        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 )
        er_result_file       r_xstring ).



Now you can use the result XSTRING do download data to application server, local pc or use it to send via e-mail.