File Handling in ABAP

 

File :-

  • File is a place where data is stored. File handling means the various operations like Opening the File, Closing the file, deleting the file, Reading the file, Writing the file.

File Handling on Presentation Server :-

  • To work with files on Presentation Server, SAP provided the various Function Modules.
    1. GUI_UPLOAD :- Reading data from a file on Presentation server.
    2. GUI_DOWNLOAD :- Writing data to a file on Presentation server.

let’s take a requirement to understand it more clearly

Requirement :-

  • We have a employee record in a notepad and we have to read the data from the notepad.



  • We will read this data and display inform of ALV.


Solution :-

  • Step 1 :- Create a executable program in ABAP Editor.



  • Step 2 :- Create a parameter of type LOCALFILE.



  • Step 3 :- Now we need to give a F4 help for our local file.

    • To achieve this purpose, we will use AT SELECTION-SCREEN on value request event.



  • Step 4 :- Now, when user will click F4 on selection screen, I want to show the presentation server of my local system.

    • So, for that purpose, I will use F4_FILENAME function module.


  • Step 5 :- We will create a type structure and a internal table for that, so that we can use it to store our data.



  • Step 6 :- We will use GUI Upload function module to upload data into the internal table.



  • Step 7 :- Now, I will use factory method of CL_SALV_TABLE class to display in form of ALV.




Code :-

*&---------------------------------------------------------------------*
*& Report ZAR_READ_FILE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZAR_READ_FILE.

TYPES : begin of ty_employee,
    EMP_ID(10) type N,
    EMP_NAME(40) type c,
  end of ty_employee.

DATA : lt_file type table of ty_employee.
DATA : 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
            .

START-OF-SELECTION.

  file = p_file.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                      = file
*     FILETYPE                      = 'ASC'
     HAS_FIELD_SEPARATOR           = 'X'
    tables
      data_tab                      = lt_file
   EXCEPTIONS
     FILE_OPEN_ERROR               = 1
     FILE_READ_ERROR               = 2
     NO_BATCH                      = 3
     GUI_REFUSE_FILETRANSFER       = 4
     INVALID_TYPE                  = 5
     NO_AUTHORITY                  = 6
     UNKNOWN_ERROR                 = 7
     BAD_DATA_FORMAT               = 8
     HEADER_NOT_ALLOWED            = 9
     SEPARATOR_NOT_ALLOWED         = 10
     HEADER_TOO_LONG               = 11
     UNKNOWN_DP_ERROR              = 12
     ACCESS_DENIED                 = 13
     DP_OUT_OF_MEMORY              = 14
     DISK_FULL                     = 15
     DP_TIMEOUT                    = 16
     OTHERS                        = 17
            .

  TRY.
  CALL METHOD cl_salv_table=>factory
    EXPORTING
      list_display   = IF_SALV_C_BOOL_SAP=>FALSE
    IMPORTING
      r_salv_table   = data(lo_alv)
    CHANGING
      t_table        = lt_file
      .
    CATCH cx_salv_msg.
  ENDTRY.
  lo_alv->display( ).

Execute the Program :-

  • Press F4 and select the file from your local system.


  • Press F8.
  • Click on allow.


Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming

Introduction to SAP