Download Data Into XLSX/MHTML File/Format - Reusable FM.

Most of the times, we are able to upload to upload XLSX file to internal table but to download data in XLSX format we struggle most of the time. Here we are presenting a simple solution to download data in XLSX/MHTML/XML format.

This function module gives you an option to select a specific filetype to be chosen. If no filetype is chosen among XLSX/MHTML/XML, then a pop up window will appear as shown below to select required filetype.

Create a simple function module as described below:




Function Module Code:

FUNCTION zdownload_file.
*"*"Local Interface:
  DATAlt_fcat    TYPE lvc_t_fcat,
        lt_data    TYPE REF TO data,
        lv_flavour TYPE string,
        lv_version TYPE string,
        lv_xmltype TYPE salv_bs_constant.

  DATAlt_choice  TYPE if_salv_bs_xml=>t_type_xml_choice,
        lwa_choice TYPE if_salv_bs_xml=>s_type_xml_choice.

  DATAlr_result  TYPE REF TO cl_salv_ex_result_data_table,
        lr_columns TYPE REF TO cl_salv_columns_table,
        lr_aggreg  TYPE REF TO cl_salv_aggregations,
        lr_table   TYPE REF TO cl_salv_table.


  GET REFERENCE OF c_data INTO lt_data.

  IF i_fieldcat[] IS INITIAL.
    ASSIGN lt_data->TO .
    TRY .
          list_display abap_false
          r_salv_table lr_table
          t_table       ).
      CATCH cx_salv_msg.

    lr_columns  lr_table->get_columns).
    lr_aggreg   lr_table->get_aggregations).
    lt_fcat     cl_salv_controller_metadata=>get_lvc_fieldcatalog(
                              r_columns      lr_columns
                              r_aggregations lr_aggreg ).
    lt_fcat[] i_fieldcat[].

  lr_result cl_salv_ex_util=>factory_result_data_table(
      r_data                      lt_data
      s_layout                    i_layout
      t_fieldcatalog              lt_fcat
      t_sort                      i_sort
      t_filter                    i_filt ).

  lv_version cl_salv_bs_a_xml_base=>get_version).
  lv_flavour if_salv_bs_c_tt=>c_tt_xml_flavour_export.
  lt_choice cl_salv_export_xml_dialog=>get_gui_spreadsheet_formats(  ).

  IF i_xlsx EQ 'X'.
    lv_xmltype if_salv_bs_xml=>c_type_xlsx.
  ELSEIF i_mhtml EQ 'X'.
    lv_xmltype if_salv_bs_xml=>c_type_mhtml.
  ELSEIF i_xml EQ 'X'.
    lv_xmltype if_salv_bs_xml=>c_type_excel_xml.

  IF lv_xmltype NE 0.
    READ TABLE lt_choice INTO lwa_choice WITH KEY xml_type lv_xmltype.
    CALL METHOD cl_salv_export_xml_dialog=>execute
        value lt_choice.

    READ TABLE lt_choice INTO lwa_choice INDEX 1.
    IF sy-subrc EQ 0.
      lv_xmltype lwa_choice-xml_type.

  CALL METHOD cl_salv_bs_tt_util=>if_salv_bs_tt_util~transform
      xml_type      lv_xmltype
      xml_version   lv_version
      r_result_data lr_result
      xml_flavour   lv_flavour
      gui_type      lwa_choice-gui_type
      xml           e_xstring.

  cl_salv_export_xml_dialog=>downloads_xml_choice lwa_choice
                                       xml e_xstring ).


If you face any issue, refer below notes:


Also Read...