Result:

Step 1: Use TCode SMW0 to upload a template file

Step 1.1: Tcode SMW0 -> F8

Step 1.2: F8

Step 1.3: F5 (Create) -> input Name + Description -> import a template file

Step 2: declare at TOP include

TABLESsscrfields.

DATAgo_get_temp TYPE REF TO zcl_get_template.

Step 3: add below row at code for design Selection Screen

SELECTION-SCREEN FUNCTION KEY 1.

Step 4: event INITIALIZATION

PERFORM set_button.

FORM set_button .
DATA smp_dyntxt TYPE smp_dyntxt.
CLEAR smp_dyntxt.
smp_dyntxttext TEXT003.”text of the button
smp_dyntxticon_id ‘@49@’.
smp_dyntxticon_text TEXT003.”text of the button
smp_dyntxtquickinfo TEXT003.”text of the button
sscrfieldsfunctxt_01 smp_dyntxt.
ENDFORM.

Step 5: event AT SELECTION-SCREEN

IF sscrfieldsucomm ‘FC01’.
PERFORM download_template.
ENDIF.

FORM download_template .
DATAls_return TYPE bapiret2.
CREATE OBJECT go_get_temp.
go_get_temp->download_temp(
EXPORTING
p_object_name =   ‘ZRE400U_TEMPLATE’               ” SAP WWW Gateway Object Name
IMPORTING
es_return     =   ls_return               ” Return Parameter
).
ENDFORM.

/////////////////////////////////////////////////////////////////////////////////////////////////

Class ZCL_GET_TEMPLATE

P_OBJECT_NAME Importing Type W3OBJID SAP WWW Gateway Object Name
ES_RETURN Exporting Type BAPIRET2 Return Parameter

METHOD download_temp.
DATAls_wwwdata_tab TYPE wwwdatatab.

CLEARes_return.

*Make sure template exists in SMw0 as binary object
SELECT FROM wwwdata INNER JOIN tadir
ON wwwdata~objid tadir~obj_name
INTO  CORRESPONDING FIELDS OF ls_wwwdata_tab UP TO ROWS
WHERE wwwdata~srtf2 0
AND   wwwdata~relid ‘MI’
AND   tadir~pgmid    ‘R3TR’
AND   tadir~object   ‘W3MI’
AND   tadir~obj_name p_object_name.
ENDSELECT.
IF sysubrc <> 0.
es_returntype ‘E’.
es_returnid ‘OO’.
es_returnnumber ‘000’.
es_returnmessage_v1 =  ‘Template’.
es_returnmessage_v2 p_object_name.
es_returnmessage_v3 =  ‘does not exist in’.
es_returnmessage_v4 =  ‘transaction SMW0’.

MESSAGE ID ‘OO’ TYPE ‘E’ NUMBER ‘000’
WITH ‘Template’ p_object_name ‘does not exist in’
INTO es_returnmessage.
*      PERFORM fill_return USING
*                                ‘E’
*                                ‘OO’
*                                ‘000’
*                                ‘Template’
*                                p_object_name
*                                ‘does not exist in’
*                                ‘transaction SMW0’
*                          CHANGING es_return.
EXIT.
ENDIF.

CALL FUNCTION ‘DOWNLOAD_WEB_OBJECT’
EXPORTING
key ls_wwwdata_tab.

ENDMETHOD.

P_OBJECT_NAME Importing Type W3OBJID
DESTINATION Importing Type LOCALFILE
ES_RETURN Exporting Type BAPIRET2

method DOWNLOAD_TEMP2.
DATAls_wwwdata_tab TYPE wwwdatatab.

CLEARes_return.

*Make sure template exists in SMw0 as binary object
SELECT FROM wwwdata INNER JOIN tadir
ON wwwdata~objid tadir~obj_name
INTO  CORRESPONDING FIELDS OF ls_wwwdata_tab UP TO ROWS
WHERE wwwdata~srtf2 0
AND   wwwdata~relid ‘MI’
AND   tadir~pgmid    ‘R3TR’
AND   tadir~object   ‘W3MI’
AND   tadir~obj_name p_object_name.
ENDSELECT.
IF sysubrc <> 0.
es_returntype ‘E’.
es_returnid ‘OO’.
es_returnnumber ‘000’.
es_returnmessage_v1 =  ‘Template’.
es_returnmessage_v2 p_object_name.
es_returnmessage_v3 =  ‘does not exist in’.
es_returnmessage_v4 =  ‘transaction SMW0’.

MESSAGE ID ‘OO’ TYPE ‘E’ NUMBER ‘000’
WITH ‘Template’ p_object_name ‘does not exist in’
INTO es_returnmessage.
*      PERFORM fill_return USING
*                                ‘E’
*                                ‘OO’
*                                ‘000’
*                                ‘Template’
*                                p_object_name
*                                ‘does not exist in’
*                                ‘transaction SMW0’
*                          CHANGING es_return.
EXIT.
ENDIF.
DATAfilename(255TYPE c.

*      sc_tmp = ‘ZST_template’.
*      CONCATENATE sc_tmp ‘_’ sy-datum INTO sc_tmp.
CALL FUNCTION ‘DOWNLOAD_WEB_OBJECT’
EXPORTING
key ls_wwwdata_tab
destination destination.

endmethod.

/////////////////////////////////////////////////////////////////////////////////////////////////

Enhancement to default file name

ENHANCEMENT 1  ZEI_DEFAULT_FNAME.    “active version
if sytcode ‘ZRE400U’.
data DEFAULT_FILE_NAME type string.
DATAfilefilter2  TYPE string,
filename2    TYPE string,
path2       TYPE string,
fullpath2    TYPE string,
user_action2 TYPE i,

cur_guicopdepage2 TYPE tcp00cpcodepage.
DEFAULT_FILE_NAME ‘ZRE400U_TEMPLATE.XLSX’.
CLASS cl_gui_frontend_services DEFINITION LOAD.

user_action2 cl_gui_frontend_services=>action_ok.
IF p_temp EQ space.              “process with user query
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
file_filter filefilter2
DEFAULT_FILE_NAME DEFAULT_FILE_NAME
CHANGING
filename    filename2
path        path2
fullpath    fullpath2
user_action user_action2.
ELSE.
CALL METHOD cl_gui_frontend_services=>GET_TEMP_DIRECTORY
changing temp_dir path2.
CALL METHOD cl_gui_cfw=>flush.
IF p_temp NE ‘f’.
CONCATENATE path2 ‘\’ p_filename INTO fullpath2.
ELSE.
fullpath2 p_filename.
ENDIF.

ENDIF.

CASE p_objtype.
WHEN ‘HT’.
IF user_action2 cl_gui_frontend_services=>action_ok.

CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
filename              fullpath2
filetype              ‘ASC’
trunc_trailing_blanks ‘X’
write_lf              ‘X’
TABLES
data_tab              html.

ENDIF.

WHEN OTHERS.
IF user_action2 cl_gui_frontend_services=>action_ok.

CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
filename     fullpath2
filetype     ‘BIN’
bin_filesize p_fsize
TABLES
data_tab     mime.

ENDIF.
ENDCASE.
p_filename fullpath2.
check 1.
endif.
ENDENHANCEMENT.