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.

        PARAMETERSp_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.

          PARAMETERSp_file TYPE localfile.

          AT SELECTION-SCREEN on VALUE-REQUEST FOR p_file.
    
        


    6. After calling Function module 'F4_FILENAME'.
            
        REPORT ZAJAY_RP_FILE_HAND.

        PARAMETERSp_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.
        


        * There is test file with name 'test.txt'.

                


    8. Read data from file using FM - '
GUI_UPLOAD'
        * Read the file and stored in internal table.

            TYPESbegin of ty_stu,
                        st_id(2type i,
                        st_name(15type c,
                      END OF ty_stu.
                datait_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).
          
              CALL FUNCTION 'GUI_UPLOAD'
              EXPORTING
                filename                      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.
                TYPESbegin of ty_stu,
                        st_id(2type i,
                        st_name(15type c,
                  END OF ty_stu.
                datait_stu type table of ty_stu,
                      wa_stu type ty_stu.
                datalv_file TYPE string.
                PARAMETERSp_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

Popular posts from this blog

New syntax for append- VALUE (new syntax 7.4+) in ABAP

Read statement new syntax in ABAP. (7.4+).

Concatenation new syntax( 7.4+ ).