14 декабря 2020

Загрузка данных из CSV-файла

Загрузка данных из CSV-файла, разделенных точкой с запятой. Используется ФМ TEXT_CONVERT_CSV_TO_SAP.

  TYPES:
    BEGIN OF ty_csv,
      id TYPE char10,
      name TYPE text50,
    END OF ty_csv.

  DATA:
    lv_str_filename TYPE string,
    lv_file_len TYPE i,
    lt_file_csv TYPE TABLE OF text4096,
    lt_csv TYPE TABLE OF ty_csv,
    ls_csv LIKE LINE OF lt_csv.

  lv_str_filename = ''. " Полное имя файла

  cl_gui_frontend_services=>gui_upload(
    EXPORTING
      filename                = lv_str_filename
      filetype                = 'ASC'
*      has_field_separator     = space
*      header_length           = 0
*      read_by_line            = 'X'
*      dat_mode                = space
*      codepage                = space
*      ignore_cerr             = abap_true
*      replacement             = '#'
*      virus_scan_profile      = 
    IMPORTING
      filelength              = lv_file_len
*      header                  =
    CHANGING
      data_tab                = lt_file_csv
    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 ).

  IF sy-subrc EQ 0.

    CALL FUNCTION 'TEXT_CONVERT_CSV_TO_SAP'
      EXPORTING
        i_field_seperator    = ';'
        i_line_header        = 'X'
        i_tab_raw_data       = lt_file_csv
*        i_filename           =
      TABLES
        i_tab_converted_data = lt_csv
      EXCEPTIONS
        conversion_failed    = 1
        OTHERS               = 2.

    IF sy-subrc EQ 0.

      LOOP AT lt_csv INTO ls_csv.
        " ToDo
      ENDLOOP.

    ENDIF.

  ENDIF.

Комментариев нет:

Отправить комментарий