SAP本地文件操作
1,本地文件路径获取
FORM GET_FILENAME. * ファイルを開くダイアログを表示FM CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG EXPORTING WINDOW_TITLE = 'ファイル選択' "ファイルを開くダイアログの表題 FILE_FILTER = 'Excel Files (*.xls)|*.xls|Text File(*.txt)|*.txt' "ファイル拡張フィルタストリング INITIAL_DIRECTORY = 'C:' "第一ディレクトリ MULTISELECTION = SPACE "複数選択可能 (X複数可能) CHANGING FILE_TABLE = LIT_FILE_NAMES "選択したファイルを維持するテーブル RC = LV_COUNT "打开文件的数量 (ファイル数かエラーの場合は -1) EXCEPTIONS FILE_OPEN_DIALOG_FAILED = 1 CNTL_ERROR = 2 ERROR_NO_GUI = 3 NOT_SUPPORTED_BY_GUI = 4 OTHERS = 5. READ TABLE LIT_FILE_NAMES INTO LWA_FILE_NAME INDEX 1. IF SY-SUBRC = 0. P_LFILE = LWA_FILE_NAME-FILENAME. ELSE. EXIT. ENDIF. ENDFORM.
※获取文件路径还有一种使用FM的方式,以及被标记为废弃,仅作参考
* 1.获取文件路径 CALL FUNCTION 'WS_FILENAME_GET' EXPORTING * DEF_FILENAME = ' ' * DEF_PATH = ' ' MASK = ',EXCEL.XLS,*.XLS.' MODE = 'O'" O保存,S打开 TITLE = '打开文件'"窗口的显示标题 IMPORTING FILENAME = LV_FILE * RC = EXCEPTIONS INV_WINSYS = 1 NO_BATCH = 2 SELECTION_CANCEL = 3 SELECTION_ERROR = 4 OTHERS = 5 . IF SY-SUBRC <> 0. EXIT. ENDIF.
1.1,点击运行按钮,选择文件路径
START-OF-SELECTION. PERFORM GET_FILENAME.
1.2,点击输入框帮助按钮,选择文件路径后在继续点击运行按钮运行程序
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE. PERFORM GET_FILENAME.
2,上传或是转换本地数据到内表
2.1,.txt文件的上传
①使用function
CALL FUNCTION 'GUI_UPLOAD' EXPORTING FILENAME = LV_FILENAME 通过1,获取到的文件路径 * FILETYPE = 'ASC' HAS_FIELD_SEPARATOR = 'X' " アップロード時は、タブで列を分割 * HEADER_LENGTH = 0 * READ_BY_LINE = 'X' * DAT_MODE = ' ' * CODEPAGE = ' ' * IGNORE_CERR = ABAP_TRUE * REPLACEMENT = '#' * CHECK_BOM = ' ' * VIRUS_SCAN_PROFILE = * NO_AUTH_CHECK = ' ' * IMPORTING * FILELENGTH = * HEADER = TABLES DATA_TAB = IT_WULIAO EXCEPTIONS FILE_OPEN_ERROR = 1 FILE_READ_ERROR = 2 NO_BATCH = 3 GUI_REFUSE_FILETRANSFER = 4 INVALID_TYPE = 5 NO_AUTHORITY = 6 UNKNOWN_ERROR = 7 BAD_DATA_FORMAT = 8 HEADER_NOT_ALLOWED = 9 SEPARATOR_NOT_ALLOWED = 10 HEADER_TOO_LONG = 11 UNKNOWN_DP_ERROR = 12 ACCESS_DENIED = 13 DP_OUT_OF_MEMORY = 14 DISK_FULL = 15 DP_TIMEOUT = 16 OTHERS = 17 . IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF.
※②使用类的方法(次方法其实内部调用的还是①,我觉得还不如直接使用①)
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD EXPORTING FILENAME = LV_PATH FILETYPE = 'ASC' HAS_FIELD_SEPARATOR = ABAP_TRUE CHANGING DATA_TAB = IT_TEMP EXCEPTIONS FILE_OPEN_ERROR = 1 FILE_READ_ERROR = 2 NO_BATCH = 3 GUI_REFUSE_FILETRANSFER = 4 INVALID_TYPE = 5 NO_AUTHORITY = 6 UNKNOWN_ERROR = 7 BAD_DATA_FORMAT = 8 HEADER_NOT_ALLOWED = 9 SEPARATOR_NOT_ALLOWED = 10 HEADER_TOO_LONG = 11 UNKNOWN_DP_ERROR = 12 ACCESS_DENIED = 13 DP_OUT_OF_MEMORY = 14 DISK_FULL = 15 DP_TIMEOUT = 16 NOT_SUPPORTED_BY_GUI = 17 ERROR_NO_GUI = 18 OTHERS = 19. IF SY-SUBRC <> 0. LEAVE LIST-PROCESSING. ENDIF.
③比较-WS_UPLOAD和GUI_UPLOAD
https://blog.csdn.net/lyq123333321/article/details/52016097
2.2,Excel的,.xls文件上传
DATA: IT_TRUXS TYPE TRUXS_T_TEXT_DATA. CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP' EXPORTING I_FIELD_SEPERATOR = 'X' " アップロード時は、タブで列を分割 I_LINE_HEADER = 'X' " ヘッダがあり I_TAB_RAW_DATA = IT_TRUXS I_FILENAME = P_LFILE TABLES I_TAB_CONVERTED_DATA = IT_WULIAO EXCEPTIONS CONVERSION_FAILED = 1 OTHERS = 2. IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF.
2.3,类似函数还有
TEXT_CONVERT_CSV_TO_SAP
TEXT_CONVERT_TEX_TO_SAP
TEXT_CONVERT_TXT_TO_SAP
TEXT_CONVERT_XLS_TO_SAP
TEXT_CONVERT_XML_TO_SAP
SAP_CONVERT_TO_CSV_FORMAT
SAP_CONVERT_TO_TEX_FORMAT
SAP_CONVERT_TO_TXT_FORMAT
SAP_CONVERT_TO_XLS_FORMAT
SAP_CONVERT_TO_XML_FORMAT
3,下载文件到本地,一般是先获取所要下载文件要保存的路径,然后调用下载方法
3.1,获取下载文件保存路径使用,1,本地文件获取的两种方法都可以。
3.2,下载方法
①使用function
CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING * BIN_FILESIZE = FILENAME = LV_FILE * FILETYPE = 'ASC' * APPEND = 'X' WRITE_FIELD_SEPARATOR = 'X' * HEADER = '00' * TRUNC_TRAILING_BLANKS = ' ' * WRITE_LF = 'X' * COL_SELECT = ' ' * COL_SELECT_MASK = ' ' * DAT_MODE = ' ' * CONFIRM_OVERWRITE = ' ' * NO_AUTH_CHECK = ' ' * CODEPAGE = ' ' * IGNORE_CERR = ABAP_TRUE * REPLACEMENT = '#' * WRITE_BOM = ' ' * TRUNC_TRAILING_BLANKS_EOL = 'X' * WK1_N_FORMAT = ' ' * WK1_N_SIZE = ' ' * WK1_T_FORMAT = ' ' * WK1_T_SIZE = ' ' * IMPORTING * FILELENGTH = TABLES DATA_TAB = IT_WULIAOTMP * FIELDNAMES = EXCEPTIONS FILE_WRITE_ERROR = 1 NO_BATCH = 2 GUI_REFUSE_FILETRANSFER = 3 INVALID_TYPE = 4 NO_AUTHORITY = 5 UNKNOWN_ERROR = 6 HEADER_NOT_ALLOWED = 7 SEPARATOR_NOT_ALLOWED = 8 FILESIZE_NOT_ALLOWED = 9 HEADER_TOO_LONG = 10 DP_ERROR_CREATE = 11 DP_ERROR_SEND = 12 DP_ERROR_WRITE = 13 UNKNOWN_DP_ERROR = 14 ACCESS_DENIED = 15 DP_OUT_OF_MEMORY = 16 DISK_FULL = 17 DP_TIMEOUT = 18 FILE_NOT_FOUND = 19 DATAPROVIDER_EXCEPTION = 20 CONTROL_FLUSH_ERROR = 21 OTHERS = 22 . IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF.
※②使用类的方法(次方法其实内部调用的还是①,我觉得还不如直接使用①)
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
4,其他
4.1,类CL_GUI_FRONTEND_SERVICES,可以对文件进行各种操作
4.2
路径名称全部转换成大写
TRANSLATE LV_FILENAME TO UPPER CASE.