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:

IMPORT:

 EXPORT:

CHANGING:


Function Module Code:

FUNCTION zdownload_file.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(I_FIELDCAT) TYPE  LVC_T_FCAT OPTIONAL
*"     REFERENCE(I_SORT) TYPE  LVC_T_SORT OPTIONAL
*"     REFERENCE(I_FILT) TYPE  LVC_T_FILT OPTIONAL
*"     REFERENCE(I_LAYOUT) TYPE  LVC_S_LAYO OPTIONAL
*"     REFERENCE(I_XLSX) TYPE  FLAG OPTIONAL
*"     REFERENCE(I_MHTML) TYPE  FLAG OPTIONAL
*"     REFERENCE(I_XML) TYPE  FLAG OPTIONAL
*"  EXPORTING
*"     REFERENCE(E_XSTRING) TYPE  XSTRING
*"  CHANGING
*"     REFERENCE(C_DATA) TYPE  STANDARD TABLE
*"----------------------------------------------------------------------
  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.

  FIELD-SYMBOLS TYPE ANY TABLE.

  GET REFERENCE OF c_data INTO lt_data.

  IF i_fieldcat[] IS INITIAL.
    ASSIGN lt_data->TO .
    TRY .
        cl_salv_table=>factory(
        EXPORTING
          list_display abap_false
        IMPORTING
          r_salv_table lr_table
        CHANGING
          t_table       ).
      CATCH cx_salv_msg.
    ENDTRY.

    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 ).
  ELSE.
    lt_fcat[] i_fieldcat[].
  ENDIF.

  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.
  ENDIF.

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

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

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

  cl_salv_export_xml_dialog=>downloads_xml_choice lwa_choice
                                       xml e_xstring ).

ENDFUNCTION.



If you face any issue, refer below notes:



Comments

Also Read...