Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
raghu_ps
Explorer

CL_GUI_FRONTEND_SERVICES is a class in the SAP ABAP programming language used for accessing front-end services in the SAP GUI (Graphical User Interface) environment. This class provides methods for various operations related to frontend services, such as displaying files, selecting files, saving files, etc. 

Here's a brief overview of some commonly used attribute and methods of CL_GUI_FRONTEND_SERVICES: 

CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG: This method opens a file dialog box that allows the user to select a file for opening. 

CL_GUI_FRONTEND_SERVICES=>FILETYPE_ALL is used to specify that all file types should be included when filtering files in file dialogs or when performing file-related operations. 

raghu_ps_0-1708944456929.png

CL_GUI_FRONTEND_SERVICES=>ACTION_OK is a constant used to specify the "OK" action in dialog boxes or pop-up windows created using GUI frontend services. This constant indicates that the user has confirmed or accepted the action. 

CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD to read the CSV file into an internal table. 

raghu_ps_1-1708944552173.png

Database Table 

raghu_ps_2-1708944591855.png

Source Code – The complete coding of the executable program is given below.

 

 

*&---------------------------------------------------------------------*
*& Report ZPS_RP_CSV_TO_ITAB
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zps_rp_csv_to_itab.

"Declare variables, types and structures

TYPES : BEGIN OF ty_s_csv,
          mandt    TYPE mandt,
          carrid   TYPE s_carr_id,
          connid   TYPE s_conn_id,
          cityfrom TYPE s_from_cit,
          cityto   TYPE s_to_city,
        END OF ty_s_csv.


TYPES : ty_it_csv TYPE STANDARD TABLE OF ty_s_csv WITH DEFAULT KEY.

    "Checkbox has the file header

PARAMETERS : p_header AS CHECKBOX DEFAULT 'X'.
TRY .

    "call file open dialog
    DATA : lv_rc     TYPE i,
           lt_files  TYPE filetable,
           lv_action TYPE i.

    cl_gui_frontend_services=>file_open_dialog(
      EXPORTING
*     WINDOW_TITLE            =                  " Title Of File Open Dialog
*     DEFAULT_EXTENSION       =                  " Default Extension
*     DEFAULT_FILENAME        =                  " Default File Name
        file_filter              =  |CSV (*.CSV)\|*.CSV\|{ cl_gui_frontend_services=>filetype_all }|          " File Extension Filter String

        multiselection          =  abap_false   " Multiple selections poss.
      CHANGING
        file_table              =   lt_files  " Table Holding Selected Files
        rc                      =   lv_rc     " Return Code, Number of Files or -1 If Error Occurred
        user_action             =   lv_action  " User Action (See Class Constants ACTION_OK, ACTION_CANCEL)

    ).

    IF lv_action = cl_gui_frontend_services=>action_ok.

      IF lines( lt_files ) = 1.
        DATA(it_csv_strings) = VALUE string_table( ).

        "read csv file to internal csv string tab

        cl_gui_frontend_services=>gui_upload(
          EXPORTING
            filename                =  CONV #( lt_files[ 1 ]-filename ) " Name of file
            filetype                = 'ASC'  " File Type (ASCII, Binary)

          CHANGING
            data_tab                =     it_csv_strings     " Transfer table for file contents
        ).

        cl_demo_output=>write_data( it_csv_strings ).

        "check if header is exists
        DATA(lv_start_line) = COND i( WHEN p_header = abap_true THEN 2 ELSE 1 ).


        "check if we havesome processable entries
        IF ( lines( it_csv_strings ) > lv_start_line - 1 ).


          "create itab csv processing
          DATA(it_csv) = VALUE ty_it_csv( ).

          LOOP AT it_csv_strings ASSIGNING FIELD-SYMBOL(<z>) FROM lv_start_line.

            DATA(ls_csv_line) = VALUE ty_s_csv( ).
            SPLIT <z> AT ';' INTO TABLE DATA(it_colums).
            IF lines( it_colums ) = 5.

              ls_csv_line-mandt = it_colums[ 1 ].
              ls_csv_line-carrid = it_colums[ 2 ].
              ls_csv_line-connid = it_colums[ 3 ].
              ls_csv_line-cityfrom = it_colums[ 4 ].
              ls_csv_line-cityto = it_colums[ 5 ].
            ENDIF.
            APPEND ls_csv_line TO it_csv.
          ENDLOOP.

          INSERT zps_t_csv_data FROM TABLE it_csv.
          IF  sy-subrc = 0.
            MESSAGE s001(zps_msg_csv).   " Records Inserted Sucessfully

          ENDIF.

          cl_demo_output=>write_data( it_csv ).

          DATA(lv_html) = cl_demo_output=>get( ).
          cl_abap_browser=>show_html(
            EXPORTING
*             HTML         =                   " HTML Table, Line Width 255 Characters
              title        =   'CSV Data'       " Window Title

              html_string  =  lv_html           " HTML String

              container     = cl_gui_container=>default_screen   " Container for display
          ).

          WRITE space.
        ENDIF.
      ENDIF.
    ENDIF.


  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.

 

 

CSV file 

raghu_ps_3-1708944889181.png

Click on check box for file header

raghu_ps_4-1708945421940.png

Click on allow for system is trying to access the file 

raghu_ps_5-1708945654701.png

Here it displays data in string and internal table format. 

raghu_ps_6-1708945716124.png

After inserting data into the database table.

raghu_ps_7-1708945748315.png