一:事物码smw0
二:上传步骤
程序源码:
l_filename = 'XX.xls'.l_muban = 'z123'.
*&---下载模板PERFORM frm_get_fullpath CHANGING gv_fullpath gv_path gv_name.
*&---路径为空则退出IF gv_fullpath IS INITIAL.MESSAGE '用户取消操作' TYPE 'S'.RETURN.ENDIF.PERFORM frm_down USING gv_fullpath.
*--------------------------------------------------------------------*
* 获取下载到本地的路径
*--------------------------------------------------------------------*
FORM frm_get_fullpath CHANGING pv_fullpath TYPE stringpv_path TYPE stringpv_name TYPE string.
DATA: lv_init_path TYPE string,lv_init_fname TYPE string,lv_path TYPE string,lv_filename TYPE string,lv_fullpath TYPE string.
*&---初始名称(输出的文件名称)
* concatenate 'Material_Doc_' SY-DATUM '.xslx' into L_INIT_FNAME.lv_init_fname = l_filename.
* 获取桌面路径CALL METHOD cl_gui_frontend_services=>get_desktop_directoryCHANGINGdesktop_directory = lv_init_pathEXCEPTIONScntl_error = 1error_no_gui = 2not_supported_by_gui = 3OTHERS = 4.IF sy-subrc <> 0.EXIT.ENDIF.
*&---用户选择名称、路径CALL METHOD cl_gui_frontend_services=>file_save_dialogEXPORTING
* window_title = '指定保存文件名'
* default_extension = 'DOC'default_file_name = lv_init_fname
* FILE_FILTER = CL_GUI_FRONTEND_SERVICES=>FILETYPE_EXCEL
* FILE_FILTER = CL_GUI_FRONTEND_SERVICES=>FILETYPE_WORDinitial_directory = lv_init_pathprompt_on_overwrite = 'X'CHANGINGfilename = lv_filenamepath = lv_pathfullpath = lv_fullpath
* USER_ACTION =
* FILE_ENCODING =EXCEPTIONScntl_error = 1error_no_gui = 2not_supported_by_gui = 3OTHERS = 4.IF sy-subrc = 0.pv_fullpath = lv_fullpath.pv_path = lv_path.ENDIF.
ENDFORM.
*--------------------------------------------------------------------*
* 下载文件
*--------------------------------------------------------------------*
FORM frm_down USING pr_filename.DATA: lv_objdata LIKE wwwdatatab,lv_mime LIKE w3mime,lv_destination LIKE rlgrap-filename,lv_objnam TYPE string,lv_rc LIKE sy-subrc,lv_errtxt TYPE string.
DATA: lv_filename TYPE string,lv_result,lv_subrc TYPE sy-subrc.
DATA: lv_objid TYPE wwwdatatab-objid .
lv_objid = l_muban. "上传的模版名称
*&---查找文件是否存在。SELECT SINGLE relid objidFROM wwwdataINTO CORRESPONDING FIELDS OF lv_objdataWHERE srtf2 = 0AND relid = 'MI'AND objid = lv_objid.
*&---判断模版不存在则报错IF sy-subrc NE 0 OR lv_objdata-objid EQ space.CONCATENATE '模板文件:' lv_objid '不存在,请用TCODE:SMW0进行加载'INTO lv_errtxt.MESSAGE lv_errtxt TYPE 'E'.ENDIF.
lv_filename = pr_filename.
"判断本地地址是否已经存在此文件。CALL METHOD cl_gui_frontend_services=>file_existEXPORTINGfile = lv_filenameRECEIVINGresult = lv_resultEXCEPTIONScntl_error = 1error_no_gui = 2wrong_parameter = 3not_supported_by_gui = 4OTHERS = 5.IF sy-subrc <> 0.CASE sy-subrc.WHEN 1.WHEN 2.WHEN OTHERS.ENDCASE.ENDIF.
IF lv_result EQ 'X'. "如果存在则删除原始文件,重新覆盖CALL METHOD cl_gui_frontend_services=>file_deleteEXPORTINGfilename = lv_filenameCHANGINGrc = lv_subrcEXCEPTIONSfile_delete_failed = 1cntl_error = 2error_no_gui = 3file_not_found = 4access_denied = 5unknown_error = 6not_supported_by_gui = 7wrong_parameter = 8OTHERS = 9.
IF sy-subrc <> 0.CASE sy-subrc.WHEN 1.WHEN 2.WHEN OTHERS.ENDCASE.ENDIF.
IF lv_subrc <> 0. "如果删除失败,则报错。CONCATENATE '同名EXCEL文件已打开' '请关闭该EXCEL后重试。'INTO lv_errtxt.MESSAGE lv_errtxt TYPE 'E'.ENDIF.ENDIF.
lv_destination = pr_filename.
*&---下载模版。CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'EXPORTINGkey = lv_objdatadestination = lv_destinationIMPORTINGrc = lv_rc.IF lv_rc NE 0.CONCATENATE '模板文件' '下载失败' INTO lv_errtxt.MESSAGE lv_errtxt TYPE 'E'.ENDIF.
ENDFORM.