该ABAP代码通过调用GUI前端服务,实现将指定的Excel文件下载到本地。
DATA:
LV_FILENAME_D TYPE STRING, "文件名
LV_PATH_D TYPE STRING, "路径
LV_FULLPATH_D TYPE STRING, "完整路径
LV_USER_ACTION TYPE I. "用户操作类型
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG "调用GUI前端服务选择保存文件对话框
EXPORTING
DEFAULT_EXTENSION = 'xlsx' "默认后缀名
DEFAULT_FILE_NAME = LC_DEFAULT_FILE_NAME "默认文件名
FILE_FILTER = 'Excel Files (.xlsx)|.xlsx' "文件过滤器
CHANGING
FILENAME = LV_FILENAME_D "返回选择的文件名
PATH = LV_PATH_D "返回选择的路径
FULLPATH = LV_FULLPATH_D "返回选择的完整路径
USER_ACTION = LV_USER_ACTION "返回用户操作类型
FILE_ENCODING =
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
INVALID_DEFAULT_FILE_NAME = 4
OTHERS = 5.
IF SY-SUBRC <> 0. "处理异常情况
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
RETURN.
ENDIF.
IF LV_USER_ACTION = 0 AND LV_FULLPATH_D IS NOT INITIAL. "如果用户点击了“保存”并且选择了一个路径
CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD( EXPORTING BIN_FILESIZE = LV_BYTECOUNT "二进制文件大小
FILENAME = LV_FULLPATH_D "文件路径和名字
FILETYPE = 'BIN' "文件类型
CHANGING DATA_TAB = LT_FILE_TAB ). "要下载的数据表
ENDIF.