设中国护士网为首页 | 中国护士网专注于知识的分享,因为专注,所以更好!
分享到:

ABAP文件下载、复制、删除实例代码

来源:SAP HR资源网  作者:小彭

*&———————————————————————*
*& Report ZTEST109
*&
*&———————————————————————*
*& CL_GUI_FRONTEND_SERVICES 的学习
*& 文件处理实例代码
*&———————————————————————*

REPORT ZTEST109 MESSAGE-ID ZHRMESSAGE.
” EXCEL 下载
DATA: CO_OBJID TYPE WWWDATATAB-OBJID ,
LC_PATH TYPE STRING,
LC_FULLPATH TYPE STRING,
LC_FILENAME TYPE STRING,
LC_FILENAME2 TYPE STRING,
P_FILENAME(128).”增加保存路径的长度

DATA RESULT(1) .
DATA :RC TYPE I.
CO_OBJID = ‘ZHRR0100′. “需要SMW0加载
LC_FILENAME = ‘D:123.xls’ .
LC_FILENAME2 = ‘D:124.xls’ .

” 保存下载文件
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
EXPORTING
DEFAULT_EXTENSION = ‘XLS’
DEFAULT_FILE_NAME = LC_FILENAME
CHANGING
FILENAME = LC_FILENAME
PATH = LC_PATH
FULLPATH = LC_FULLPATH
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.

IF LC_FULLPATH = ”.
RETURN.
ENDIF.

IF SY-SUBRC = 0.

P_FILENAME = LC_FULLPATH.

PERFORM FRM_DOWNLOAD_EXCEL_FROMSERVER USING CO_OBJID P_FILENAME.

ENDIF.
” 检查文件是否存在
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_EXIST
EXPORTING
FILE = LC_FILENAME
RECEIVING
RESULT = RESULT
* EXCEPTIONS
* CNTL_ERROR = 1
* ERROR_NO_GUI = 2
* WRONG_PARAMETER = 3
* NOT_SUPPORTED_BY_GUI = 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.
ENDIF.

IF RESULT IS INITIAL.
MESSAGE ‘文件不存在,请检查!’ TYPE ‘I’.
EXIT.
ELSE.
MESSAGE ‘文件存在,SUCCESS!’ TYPE ‘I’.
ENDIF.

” 打开文件
DATA : FILE_TABLE TYPE STANDARD TABLE OF FILE_TABLE .
DATA : WA_FILE_TABLE TYPE FILE_TABLE .
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
* EXPORTING
* WINDOW_TITLE =
* DEFAULT_EXTENSION =
* DEFAULT_FILENAME =
* FILE_FILTER =
* WITH_ENCODING =
* INITIAL_DIRECTORY =
* MULTISELECTION =
CHANGING
FILE_TABLE = FILE_TABLE
RC = RC
* USER_ACTION =
* FILE_ENCODING =
* EXCEPTIONS
* FILE_OPEN_DIALOG_FAILED = 1
* CNTL_ERROR = 2
* ERROR_NO_GUI = 3
* NOT_SUPPORTED_BY_GUI = 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.
ENDIF.

CLEAR LC_FILENAME.
LOOP AT FILE_TABLE INTO WA_FILE_TABLE.
LC_FILENAME = WA_FILE_TABLE-FILENAME.
ENDLOOP.

” 复制文件
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_COPY
EXPORTING
SOURCE = LC_FILENAME
DESTINATION = LC_FILENAME2
* OVERWRITE = SPACE
* EXCEPTIONS
* CNTL_ERROR = 1
* ERROR_NO_GUI = 2
* WRONG_PARAMETER = 3
* DISK_FULL = 4
* ACCESS_DENIED = 5
* FILE_NOT_FOUND = 6
* DESTINATION_EXISTS = 7
* UNKNOWN_ERROR = 8
* PATH_NOT_FOUND = 9
* DISK_WRITE_PROTECT = 10
* DRIVE_NOT_READY = 11
* NOT_SUPPORTED_BY_GUI = 12
* OTHERS = 13
.
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=>FILE_DELETE
EXPORTING
FILENAME = LC_FILENAME
CHANGING
RC = RC
* EXCEPTIONS
* FILE_DELETE_FAILED = 1
* CNTL_ERROR = 2
* ERROR_NO_GUI = 3
* FILE_NOT_FOUND = 4
* ACCESS_DENIED = 5
* UNKNOWN_ERROR = 6
* NOT_SUPPORTED_BY_GUI = 7
* WRONG_PARAMETER = 8
* OTHERS = 9
.
IF SY-SUBRC 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
*&———————————————————————*
*& Form FRM_DOWNLOAD_EXCEL_FROMSERVER
*&———————————————————————*
* text
*———————————————————————-*
* –>P_CO_OBJID text
* –>P_P_FILENAME text
*———————————————————————-*
FORM FRM_DOWNLOAD_EXCEL_FROMSERVER USING P_OBJID LIKE WWWDATATAB-OBJID
P_DEST.
DATA: LO_OBJDATA LIKE WWWDATATAB,
LO_MIME LIKE W3MIME,
LS_DESTINATION LIKE RLGRAP-FILENAME,
LS_OBJNAM TYPE STRING,
LI_RC LIKE SY-SUBRC,
LS_ERRTXT TYPE STRING.

CONCATENATE P_OBJID ‘.XLS’ INTO LS_OBJNAM.
CONDENSE LS_OBJNAM NO-GAPS.

SELECT SINGLE RELID OBJID FROM WWWDATA
INTO CORRESPONDING FIELDS OF LO_OBJDATA
WHERE SRTF2 = 0
AND RELID = ‘MI’
AND OBJID = P_OBJID.

IF SY-SUBRC NE 0 OR LO_OBJDATA-OBJID EQ SPACE.
CONCATENATE ‘模板文件:’ LS_OBJNAM ‘不存在,请用TCODE:SMW0进行加载’ INTO LS_ERRTXT.
MESSAGE E001(ZHRMESSAGE) WITH LS_ERRTXT.
* WRITE: LS_ERRTXT.
ENDIF.
CLEAR LS_ERRTXT.
LS_DESTINATION = P_DEST.

CALL FUNCTION ‘DOWNLOAD_WEB_OBJECT’
EXPORTING
KEY = LO_OBJDATA
DESTINATION = LS_DESTINATION
IMPORTING
RC = LI_RC.

IF LI_RC NE 0.
CONCATENATE ‘模板文件:’ LS_OBJNAM ‘下载失败’ INTO LS_ERRTXT.
MESSAGE E001(ZHRMESSAGE) WITH LS_ERRTXT.
* WRITE: LS_ERRTXT.
ENDIF.
ENDFORM. ” FRM_DOWNLOAD_EXCEL_FROMSERVER