Upload file with UTF8 or ANSi encoding

Upload file with UTF8 or ANSi encoding

EDIT : Warning, solution below will work only if there is an accentuated caracter in first line.

Wrong encoding format file is a reccurent issue, especially when working on Windows.
If you don’t want to impose to end used an encoding format for text files, you can use the code below.


  DATA: lv_filename TYPE string,
        lt_lines    TYPE stringtab.

  lv_filename = p_file. " Form selection screen

  " Get file content encoded in UTF8
  cl_gui_frontend_services=>gui_upload(
    EXPORTING
      filename                = lv_filename
      codepage                = '4110'
    CHANGING
      data_tab                = lt_lines
    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 <> 0.
    " Display FM error message
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

  ELSE.
    " If header line contains # character, it is not UTF8 format
    DATA(ls_line) = lt_lines[ 1 ] .
    FIND '#' IN ls_line.

    IF sy-subrc IS INITIAL.
      " Get file content encoded in UTF8 ANSi
      cl_gui_frontend_services=>gui_upload(
        EXPORTING
          filename                = lv_filename
        CHANGING
          data_tab                = lt_lines
        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 <> 0.
       " Display FM error message
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

      ENDIF.

    ENDIF.

  ENDIF.

About the author

fjourneau administrator

Hi, I'm Florian Journeau, SAP ABAP R3 Freelance, based in Toulouse, France. You want to know more about me, have a look on my CV : cv.fjourneau.net.

Leave a Reply