Wednesday, March 18, 2020

Upload text file to internal table in SAP ABAP.



Upload text file to internal table in SAP ABAP.


Upload text file to internal table.

It was explained how to get all the file from the local system to fetch the text file to be uploaded. Well, This post is an example of using the function module to process text files that are uploaded into the internal table. The function used for uploading is GUI_UPLOAD.

To upload the flat text passing parameter file 'ASC', while the delimited tab use 'DAT'.

*&---------------------------------------------------------------------*
*&      Data Declaration
*&---------------------------------------------------------------------*
TABLES: rlgrap.

TYPES: BEGIN OF ty_item,
        ebeln LIKE ekpo-ebeln,
        ebelp LIKE ekpo-ebelp,
      END OF ty_item.

DATA: t_item TYPE STANDARD TABLE OF ty_item WITH HEADER LINE.

SELECT-OPTIONS: s_file FOR rlgrap-filename NO INTERVALS NO-EXTENSION.

*&---------------------------------------------------------------------*
*&      Events
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_file-low.
  PERFORM f_help_for_file CHANGING s_file-low.

START-OF-SELECTION.
  PERFORM f_upload_data TABLES t_item.

  LOOP AT t_item.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = t_item-ebeln
      IMPORTING
        output = t_item-ebeln.
    MODIFY t_item.
  ENDLOOP.
*&---------------------------------------------------------------------*
*&      Form  f_help_for_file
*&---------------------------------------------------------------------*
FORM f_help_for_file  CHANGING s_file-low.

  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    EXPORTING
      static    = 'X'
    CHANGING
      file_name = s_file-low.

ENDFORM.                    " F_HELP_FOR_FILE

*&---------------------------------------------------------------------*
*&      Form  f_upload_data
*&---------------------------------------------------------------------*
FORM f_upload_data TABLES ft_table.
  DATA: ld_string TYPE string.

  ld_string = s_file-low.
  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                = ld_string
      filetype                = 'ASC'
      has_field_separator     = ' '
    TABLES
      data_tab                = ft_table
    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.
  IF sy-subrc NE 0.
    WRITE: 'error', sy-subrc.
    STOP.
  ENDIF.

ENDFORM.                    " F_UPLOAD_DATA

No comments:

Post a Comment