KCD_CSV_FILE_TO_INTERN_CONVERT: Upload CSV File in ABAP

First open a new Excel file and enter the data that you want to upload from SAP presentation server. Then save the file in comma separated format (CSV). Use the function module KCD_CSV_FILE_TO_INTERN_CONVERT to upload the CSV file data to an internal table.

TYPE-POOLS: kcde.

TYPES: BEGIN OF ty_input,
       id     TYPE string,
       name   TYPE string,
       place  TYPE string,
       phone  TYPE string,
       END OF ty_input.

CONSTANTS: c_separator TYPE c VALUE ','.

DATA: gt_intern    TYPE kcde_intern,
      gwa_intern   TYPE kcde_intern_struc.

DATA: gt_input     TYPE TABLE OF ty_input,
      gwa_input    TYPE ty_input.
*----------------------------------------------------------------------*
*     SELECTION-SCREEN
*----------------------------------------------------------------------*
PARAMETERS: p_file TYPE localfile OBLIGATORY.
*----------------------------------------------------------------------*
*     AT SELECTION-SCREEN ON VALUE-REQUEST
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*F4 help for the file in selection screen
  CALL FUNCTION 'F4_FILENAME'
    IMPORTING
      file_name = p_file.
*----------------------------------------------------------------------*
*     START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
*Read data from File
  PERFORM readfile USING p_file.
*Display data
  PERFORM display.
*&---------------------------------------------------------------------*
*&      Form  READFILE
*&---------------------------------------------------------------------*
FORM readfile USING p_file TYPE localfile.

  DATA: lv_filename TYPE rlgrap-filename.
  DATA: lv_index TYPE i.
  FIELD-SYMBOLS:  TYPE ANY.

  lv_filename = p_file.

*Read the CSV file data to an internal table
  CALL FUNCTION 'KCD_CSV_FILE_TO_INTERN_CONVERT'
    EXPORTING
      i_filename      = lv_filename
      i_separator     = c_separator
    TABLES
      e_intern        = gt_intern
    EXCEPTIONS
      upload_csv      = 1
      upload_filetype = 2
      OTHERS          = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  LOOP AT gt_intern INTO gwa_intern.
    MOVE : gwa_intern-col TO lv_index.
    ASSIGN COMPONENT lv_index OF STRUCTURE gwa_input TO .
    MOVE : gwa_intern-value TO .
    AT END OF row.
      APPEND gwa_input TO gt_input.
      CLEAR gwa_input.
    ENDAT.
  ENDLOOP.

ENDFORM.                    " READFILE
*&---------------------------------------------------------------------*
*&      Form  DISPLAY
*&---------------------------------------------------------------------*
FORM display.
*Display internal table data
  LOOP AT gt_input INTO gwa_input.
    WRITE:/ gwa_input-id,8 gwa_input-name,16
            gwa_input-place,26 gwa_input-phone.
  ENDLOOP.
ENDFORM.                    " DISPLAY

Input File

abap-upload-csv-file-1

Output

abap-upload-csv-file-2


2 Comments

Comments are closed.