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
TABLES: sscrfields.
DATA: go_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_dyntxt–text = TEXT–003.”text of the button
smp_dyntxt–icon_id = ‘@49@’.
smp_dyntxt–icon_text = TEXT–003.”text of the button
smp_dyntxt–quickinfo = TEXT–003.”text of the button
sscrfields–functxt_01 = smp_dyntxt.
ENDFORM.
Step 5: event AT SELECTION-SCREEN
IF sscrfields–ucomm = ‘FC01’.
PERFORM download_template.
ENDIF.
FORM download_template .
DATA: ls_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.
DATA: ls_wwwdata_tab TYPE wwwdatatab.
CLEAR: es_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 1 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 sy–subrc <> 0.
es_return–type = ‘E’.
es_return–id = ‘OO’.
es_return–number = ‘000’.
es_return–message_v1 = ‘Template’.
es_return–message_v2 = p_object_name.
es_return–message_v3 = ‘does not exist in’.
es_return–message_v4 = ‘transaction SMW0’.
MESSAGE ID ‘OO’ TYPE ‘E’ NUMBER ‘000’
WITH ‘Template’ p_object_name ‘does not exist in’
INTO es_return–message.
* 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.
DATA: ls_wwwdata_tab TYPE wwwdatatab.
CLEAR: es_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 1 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 sy–subrc <> 0.
es_return–type = ‘E’.
es_return–id = ‘OO’.
es_return–number = ‘000’.
es_return–message_v1 = ‘Template’.
es_return–message_v2 = p_object_name.
es_return–message_v3 = ‘does not exist in’.
es_return–message_v4 = ‘transaction SMW0’.
MESSAGE ID ‘OO’ TYPE ‘E’ NUMBER ‘000’
WITH ‘Template’ p_object_name ‘does not exist in’
INTO es_return–message.
* PERFORM fill_return USING
* ‘E’
* ‘OO’
* ‘000’
* ‘Template’
* p_object_name
* ‘does not exist in’
* ‘transaction SMW0’
* CHANGING es_return.
EXIT.
ENDIF.
DATA: filename(255) TYPE 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 sy–tcode = ‘ZRE400U’.
data DEFAULT_FILE_NAME type string.
DATA: filefilter2 TYPE string,
filename2 TYPE string,
path2 TYPE string,
fullpath2 TYPE string,
user_action2 TYPE i,
cur_guicopdepage2 TYPE tcp00–cpcodepage.
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 0 = 1.
endif.
ENDENHANCEMENT.