Upload Text File

Program & Report

ตัวอย่าง Code สำหรับ Upload Text File สามารถ Copy ไปประยุกต์ใช้ได้เลยนะครับ

data: LF_FILE type STRING,
LF_FILELENGTH type I.

parameters: P_FILE type LOCALFILE default ”.

************************************************************************
* Events
************************************************************************
at selection-screen on value-request for P_FILE.
perform FILE_F4.

start-of-selection.
if P_FILE is initial.
message S058(VSCAN) display like ‘E’.
exit. ” =================== EXIT =====================
endif.

LF_FILE = P_FILE.

call function ‘GUI_UPLOAD’
exporting
FILENAME = LF_FILE
FILETYPE = ‘ASC’
* HAS_FIELD_SEPARATOR = ‘X’ ” ถ้า Upload เป็นแบบ DAT
” ให้เปิด comment นี้
* DAT_MODE = ‘X’ ” ถ้า Upload เป็นแบบ DAT
” ให้เปิด comment นี้
* HEADER_LENGTH = 0
* READ_BY_LINE = ‘X’
CODEPAGE = ‘8600’
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = ‘#’
* CHECK_BOM = ‘ ‘
* VIRUS_SCAN_PROFILE =
* NO_AUTH_CHECK = ‘ ‘
importing
FILELENGTH = LF_FILELENGTH
tables
DATA_TAB = ITAB_EXTPR1.

if SY-SUBRC <> 0.
message id SY-MSGID type ‘S’ number SY-MSGNO
display like ‘E’
with SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
exit. ” =================== EXIT =====================
endif.

*&———————————————————————*
*& Form FILE_F4
*&———————————————————————*
* F4-help for filename
*———————————————————————-*
form FILE_F4.
data: LT_FILETABLE type FILETABLE,
LF_RC type I.

call method CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
exporting
MULTISELECTION = ABAP_FALSE
changing
FILE_TABLE = LT_FILETABLE
RC = LF_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.
message id SY-MSGID type ‘S’ number SY-MSGNO
display like ‘E’
with SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
exit.
endif.

* Number of selected filed must be equal to one.
check LF_RC = 1.

* Access selected file
data:
LS_FILE type FILE_TABLE.

read table LT_FILETABLE into LS_FILE index 1.
check SY-SUBRC = 0.

P_FILE = LS_FILE-FILENAME.

endform. “FILE_F4