Skip to Content

Simple program to download excel in MHTML format from internal table ( ex: can be used in case of downloading icons,subtotals, coloring etc ).

This program is just a sample created for my purpose,please do further research as required. The below code can be used just in case of custom requirements if any. The method factory_result_data_table has many parameters where in we can format the data to be displayed.

REPORT  ymhtml_download.
*----------------------------------------------------------------------*
*       CLASS lcl_local DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_local DEFINITION INHERITING FROM cl_gui_alv_grid.
  PUBLIC SECTION.
    METHODS: provide_gui_type RETURNING value(lv_type) TYPE i,
             constructor IMPORTING i_parent TYPE REF TO cl_gui_container.
ENDCLASS.                    "lcl_local DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_local IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_local IMPLEMENTATION.
  METHOD provide_gui_type.
    lv_type = get_gui_type( ).
  ENDMETHOD.                    "get_gui_type
  METHOD constructor.
    super->constructor( i_parent = i_parent ).
  ENDMETHOD.                    "constructor
ENDCLASS.                    "lcl_local IMPLEMENTATION
DATA:it_output TYPE TABLE OF ymhtml_output,
     ls_layout TYPE lvc_s_layo,
     lt_fcat TYPE lvc_t_fcat,
     lt_sort TYPE lvc_t_sort,
     lt_filter TYPE lvc_t_filt,
     lv_version TYPE string,
     lv_display_mode TYPE i,
     lv_gui_type TYPE i,
     lv_xml TYPE xstring,
     lv_value TYPE salv_gzt_admin_value,
     lr_container TYPE REF TO cl_gui_container,
     ls_xml_choice TYPE if_salv_bs_xml=>s_type_xml_choice,
     lt_xml_choice TYPE if_salv_bs_xml=>t_type_xml_choice.
DATA:lr_result_data TYPE REF TO cl_salv_ex_result_data_table,
     lr_controller TYPE REF TO cl_salv_export_c8r,
     lr_data TYPE REF TO data,
     lr_obj TYPE REF TO lcl_local.
FIELD-SYMBOLS:<fs_wa> TYPE ymhtml_output,
              <fs_fcat> TYPE lvc_s_fcat.
START-OF-SELECTION.
  SELECT * FROM ekpo INTO CORRESPONDING FIELDS OF TABLE it_output UP TO 10 ROWS.
  IF it_output[] IS INITIAL.
    EXIT.
  ENDIF.
  LOOP AT it_output ASSIGNING <fs_wa>.
    <fs_wa>-icon1 = '@01@'.
    <fs_wa>-icon2 = '@06@'.
    <fs_wa>-value = sy-tabix + 1.
  ENDLOOP.
  GET REFERENCE OF it_output[] INTO lr_data.
  ls_layout-cwidth_opt = abap_true.
  ls_layout-zebra = 'X'.
  CREATE OBJECT lr_obj
    EXPORTING
      i_parent = lr_container.
  lv_version = cl_salv_bs_a_xml_base=>get_version( ).
  IF lv_version IS INITIAL.
    lv_version = if_salv_bs_xml=>version.
  ENDIF.
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name   = 'YMHTML_OUTPUT'
      i_bypassing_buffer = 'X'
    CHANGING
      ct_fieldcat        = lt_fcat.
  IF lt_fcat[] IS NOT INITIAL.
    READ TABLE lt_fcat ASSIGNING <fs_fcat> WITH KEY fieldname = 'ICON1'.
    IF sy-subrc = 0.
      <fs_fcat>-icon = 'X'.
    ENDIF.
    READ TABLE lt_fcat ASSIGNING <fs_fcat> WITH KEY fieldname = 'ICON2'.
    IF sy-subrc = 0.
      <fs_fcat>-icon = 'X'.
    ENDIF.
    READ TABLE lt_fcat ASSIGNING <fs_fcat> WITH KEY fieldname = 'VALUE'.
    IF sy-subrc = 0.
      <fs_fcat>-do_sum = 'X'.
    ENDIF.
  ENDIF.
 lv_value = abap_true.
  cl_salv_gzt_admin=>set_value( EXPORTING
          object = cl_salv_gzt_admin=>c_param-prevent_export_format_perso
        value  = lv_value ).
 lt_xml_choice =
          cl_salv_export_xml_dialog=>execute( gui_type = lr_obj->provide_gui_type( )
                          display_mode = cl_salv_export_xml_dialog=>c_display_mode-menu_item ).
*-Set popup state to previous
  lv_value = abap_false.
  cl_salv_gzt_admin=>set_value( EXPORTING
                    object = cl_salv_gzt_admin=>c_param-prevent_export_format_perso
                value  = lv_value ).
*-
  READ TABLE lt_xml_choice INTO ls_xml_choice INDEX 1.
  IF sy-subrc NE 0.
    EXIT.
  ENDIF.
  lr_result_data = cl_salv_ex_util=>factory_result_data_table( r_data          = lr_data
                                                               t_fieldcatalog  = lt_fcat
                                                               s_layout        = ls_layout ).
  cl_salv_bs_tt_util=>if_salv_bs_tt_util~transform( EXPORTING 
                                                            xml_type      = ls_xml_choice-xml_type
                                                xml_version   = lv_version
                                                r_result_data = lr_result_data
                                                xml_flavour   = if_salv_bs_c_tt=>c_tt_xml_flavour_export
                                                gui_type      = ls_xml_choice-gui_type
                                                    IMPORTING 
                                                            xml           = lv_xml ).
  cl_salv_export_xml_dialog=>download( s_xml_choice = ls_xml_choice xml = lv_xml ).
To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply