Read data from a file on presentation server
Basic file handling program
1. Goto -SE38.
2. Give program name and title and select type executable program.
3. write code like this.
REPORT ZAJAY_RP_FILE_HAND.
PARAMETERS: p_file TYPE localfile. 4. Execute the code.
* There is no serch help option.
5. After write this code for F4.
REPORT ZAJAY_RP_FILE_HAND.
PARAMETERS: p_file TYPE localfile.
AT SELECTION-SCREEN on VALUE-REQUEST FOR p_file.
6. After calling Function module 'F4_FILENAME'.
REPORT ZAJAY_RP_FILE_HAND.
PARAMETERS: p_file TYPE localfile.
AT SELECTION-SCREEN on VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
PROGRAM_NAME = SYST-CPROG
DYNPRO_NUMBER = SYST-DYNNR
FIELD_NAME = ' '
IMPORTING
FILE_NAME = p_file.
7. Execute the above code click on help button then select the file it will give the full path.
st_name(15) type c,
END OF ty_stu.
data: it_stu type table of ty_stu,
wa_stu type ty_stu.
* There is test file with name 'test.txt'.
* Read the file and stored in internal table.
TYPES: begin of ty_stu,
st_id(2) type i,st_name(15) type c,
END OF ty_stu.
data: it_stu type table of ty_stu,
wa_stu type ty_stu.
* pass the file name and internal table to FM.
* If you use tab in file then give true field separator is true(X).
filename = p_file
HAS_FIELD_SEPARATOR = 'X'
tables
data_tab = it_stu.
wa_stu-st_name UNDER 'Student name'.
ENDLOOP.
st_id(2) type i,
st_name(15) type c,
END OF ty_stu.
data: it_stu type table of ty_stu,
wa_stu type ty_stu.
data: lv_file TYPE string.
PARAMETERS: p_file TYPE localfile.
AT SELECTION-SCREEN on VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
PROGRAM_NAME = SYST-CPROG
DYNPRO_NUMBER = SYST-DYNNR
FIELD_NAME = ' '
IMPORTING
FILE_NAME = p_file.
lv_file = p_file. " Implicit type casting
START-OF-SELECTION.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = lv_file
HAS_FIELD_SEPARATOR = 'X'
tables
data_tab = it_stu.
LOOP AT it_stu INTO wa_stu.
write: / wa_stu-st_id UNDER 'Student ID',
wa_stu-st_name UNDER 'Student name'.
ENDLOOP.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTINGfilename = p_file
HAS_FIELD_SEPARATOR = 'X'
tables
data_tab = it_stu.
* Display data.
LOOP AT it_stu into wa_stu.
write: / wa_stu-st_id UNDER 'Student ID',wa_stu-st_name UNDER 'Student name'.
ENDLOOP.
9. Click on execute and select file.
* It will raise type conflit error.
* Because file name is string but P_file is char.
* Do type casting and pass new file name.
data: lv_file type string.
lv_file = p_file. " Implicit type casting
10. Full code.
REPORT ZAJAY_RP_FILE_HAND.
TYPES: begin of ty_stu,st_id(2) type i,
st_name(15) type c,
END OF ty_stu.
data: it_stu type table of ty_stu,
wa_stu type ty_stu.
data: lv_file TYPE string.
PARAMETERS: p_file TYPE localfile.
AT SELECTION-SCREEN on VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
PROGRAM_NAME = SYST-CPROG
DYNPRO_NUMBER = SYST-DYNNR
FIELD_NAME = ' '
IMPORTING
FILE_NAME = p_file.
lv_file = p_file. " Implicit type casting
START-OF-SELECTION.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = lv_file
HAS_FIELD_SEPARATOR = 'X'
tables
data_tab = it_stu.
LOOP AT it_stu INTO wa_stu.
write: / wa_stu-st_id UNDER 'Student ID',
wa_stu-st_name UNDER 'Student name'.
ENDLOOP.
11. Output.
Comments
Post a Comment