Reading File Using Methods Of CL_GUI_FRONTEND SERVICES

 

  • CL_GUI_FRONTEND SERVICES is one of the important classes provided by SAP in object oriented concepts.

  • Let’s take a requirement, suppose, we have a employee data

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

  • We will use methods of CL_GUI_FRONTEND_SERVICES to achieve this requirement.


Hint :-

  • We will use FILE_OPEN_DIALOG method to select a file.
  • then we will use GUI_UPLOAD METHOD to upload data.
  • then we will display it in form of ALV.

Solution :-

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

  • Step 2 :- Create a type structure for the data which you want to upload.



  • Step 3 :- In start of selection use method FILE_OPEN_DIALOG to select data from your PC.



  • Step :- Use GUI_UPLOAD method of CL_GUI_FRONTEND_SERVICES to upload data.



  • Step 5 :- Now our data is stored into internal table of file, so we need to display it in a alv, so we will use CL_SALV_TABLE to display.




Code :-

*&---------------------------------------------------------------------*
*& Report ZAR_FILE_UPLOAD
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZAR_FILE_UPLOAD.

types : begin of ty_file,
    emp_id(3) type n,
    emp_name(40) type c,
  end of ty_file.

  DATA : lt_file type table of ty_file.
  DATA : lo_file type string.

  START-OF-SELECTION.
DATA : FILE_TABLE type table of FILE_TABLE.
DATA : lo_rc type i.
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    CHANGING
      file_table              = FILE_TABLE
      rc                      = lo_rc
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      others                  = 5
          .

  Read table FILE_TABLE into data(ls_file_table) index 1.
  if sy-subrc eq 0.

  lo_file = ls_file_table-filename.
  endif.

  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                = lo_file
*      filetype                = 'ASC'
      has_field_separator     = 'X'
*      header_length           = 0
    CHANGING
      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
      not_supported_by_gui    = 17
      error_no_gui            = 18
      others                  = 19
          .

  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 Code :-

  • As soon as you will execute the code, it will navigate to your local system where you will select the file.
  • Select the file click on allow button.


  • data in form of alv.

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