1. 普通方法
CALL FUNCTION 'WS_FILENAME_GET' "用于选择本地文件,获取本地文件的路径
EXPORTING
* DEF_FILENAME = ' ' "默认的文件名称
* DEF_PATH = ' ' "默认的文件地址
* MASK = ',EXCEL.XLS,*.XLSX,*.XLS,TEXT.TXT,*.TXT.'
MODE = 'O' "S 保存 O 打开
TITLE = '选择文件' "窗口的显示名称
IMPORTING "传出给选择屏幕上的文本框
FILENAME = p_fileS
* RC =
EXCEPTIONS
INV_WINSYS = 1
NO_BATCH = 2
SELECTION_CANCEL = 3
SELECTION_ERROR = 4
OTHERS = 5
.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
2.面向对象方法
DATA: lit_file TYPE filetable,
l_file_filter TYPE string,
l_rc TYPE i.
FIELD-SYMBOLS: <fs_file> LIKE LINE OF lit_file.
l_file_filter = 'Excel Template (*.xlt)|*.xlt|Excel Workbook (*.xls)|*.xls|All files (*.*)|*.*'.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = 'Choose template file ...'
default_extension = '*.*'
"file_filter = l_file_filter
CHANGING
file_table = lit_file
rc = l_rc
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc = 0.
READ TABLE lit_file ASSIGNING <fs_file> INDEX 1.
IF sy-subrc = 0.
file = <fs_file>-filename.
ENDIF.
ENDIF.