ABAP Code:การนำข้อมูล Text File จากข้างนอก มาเก็บไว้ใน Internal Table

สืบเนื่องจากได้รับหน้าที่เขียนโปรแกรม ที่มีการโหลดไฟล์จากคอมพิวเตอร์ (ไฟล์ .txt) แล้วเอาข้อมูล
มาเช็คในระบบว่ามีอยู่หรือไม่และตรงกันหรือไหม หลังจากนั้น ให้แสดงออกมาเป็นรายงาน ซึ่งหลักการก็
นำไฟล์จากข้างนอกมาใส่ใน Internal Table แล้วก็ดึงข้อมูลจากระบบมาไว้ใน Internal Table อีกอัน แล้วเอา
ทั้ง 2 Internal Table มาเช็คกัน แต่ในที่นี้ เป็น Code สำหรับการนำข้อมูลที่ได้โหลดจากคอมพิวเตอร์มาเก็บไว้
ใน Internal Table

*  Program      : ZGETFILE
*  Title        : Get file to internal table
*  Author       : Todsapon Kumnuan (todsapon.com)
*  R/3 Release  : ECC5

REPORT ztestpon
MESSAGE-ID fi.

*   I N T E R N A L   T A B L E S
TYPES: BEGIN OF t_import_file,
data(1000),
END   OF t_import_file.

TYPES: BEGIN OF t_record_file,
001(50),
002(50),
003(50),
004(50),
END OF t_record_file.

*   DATA
DATA: i_record_file    TYPE TABLE OF t_record_file           WITH HEADER LINE,
i_import_file   TYPE TABLE OF t_import_file            WITH HEADER LINE.

* UPLOAD_FILE_LOCAL
* Used to upload external file from presentation server to internal
* table
*  -->  &1        file name
*  -->  &2        file type 'DAT' 'ASC'
*  -->  &3        internal table
*--> เรียกใช้ฟังก์ชั่น Upload
DEFINE upload_file_local.
call function 'WS_UPLOAD'
exporting
filename                = &1
filetype                = &2
tables
data_tab                = &3
exceptions
file_open_error         = 1
file_read_error         = 2
no_batch                = 3
gui_refuse_filetransfer = 4
others                  = 5.
if sy-subrc <> 0.
if sy-subrc = 1.
message i000(38) with 'File not found.' &1 &2 &3.
else.
message i000(38) with 'Error uploading file.' &1 &2 &3.
endif.
endif.

END-OF-DEFINITION.

*   S E L E C T - O P T I O N S   A N D   P A R A M E T E R S
PARAMETERS: p_file  LIKE rlgrap-filename OBLIGATORY.

*               A T   S E L E C T I O N - S C R E E N
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*--> เรียกใช้ฟังก์ชั่น เลือกไฟล์
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
mask      = ',*.txt,*.*'
static    = 'X'
CHANGING
file_name = p_file.

START-OF-SELECTION.
PERFORM get_data.
PERFORM display_report.

*                    S U B   R O U T I N E S
*      Form  get_data
FORM get_data.
upload_file_local p_file 'ASC' i_import_file.
*--> ตรวจสอบข้อมูลไฟล์ ว่ามีค่าว่างหรือไหม
IF i_import_file[] IS INITIAL.
MESSAGE i899 WITH 'No data on this file'.
ELSE.
PERFORM create_file.
ENDIF.
ENDFORM.                    " get_data

*&      Form  create_rtndata
FORM create_file.
*--> นำข็อมูลไฟล์ที่ได้จากการหน้าจอมาใส่ใน Internal Table
CLEAR: i_import_file, i_record_file.
LOOP AT i_import_file.
*--> โดยการใช้คอมมา เป็นการแบ่งคอลัมน์
SPLIT i_import_file-data AT ',' INTO i_record_file-001 i_record_file-002
i_record_file-003 i_record_file-004
.
CONDENSE: i_record_file-001, i_record_file-002, i_record_file-003,
i_record_file-004.
APPEND i_record_file.
CLEAR  i_record_file.
ENDLOOP.
ENDFORM.                    " create_file

*&      Form  display_report
FORM  display_report.
LOOP AT i_record_file.
WRITE: / i_record_file-001, i_record_file-002, i_record_file-003,
i_record_file-004.
ENDLOOP.
ENDFORM.                    "display_report