Import CSV File - in SAP ABAP [english]

Поділитися
Вставка
  • Опубліковано 14 гру 2024

КОМЕНТАРІ • 8

  • @SYL-h9w
    @SYL-h9w 2 роки тому

    It's really helped a lot!! Thanks for the great video!!! I like your video so much!

    • @CustAndCode
      @CustAndCode  2 роки тому

      Thank you! 😃 Glad to hear that! 🤗

  • @79rooky
    @79rooky 2 роки тому +1

    Great video
    I'm doing this at the moment but mine needs to accept any csv file so I need to put the code into a dynamic internal table
    Any ideas on how to do that?

    • @CustAndCode
      @CustAndCode  2 роки тому

      Do you select your file from your local computer or from the application server? I am not sure yet what exactly your requirement is. Do you want to upload the CSV without manual confirmation? 🤔

  • @sk16034
    @sk16034 15 днів тому

    How do we import file from application server and access it in the program? Can you help me on this requirement?

    • @CustAndCode
      @CustAndCode  9 днів тому

      I already did a video where I fetch some filled from the application server. If you have done this you can access the data in the file 🤗

  • @vashisthnihit
    @vashisthnihit 2 роки тому

    could you please copy paste the code in comment section, or share some text file is having this? I want to try similar case

    • @CustAndCode
      @CustAndCode  2 роки тому +2

      Best way is to write it 😉 it is helpful for the understanding! 😊 but here it is:
      ----- Code Begin -----
      " declare variables, types and structures
      TYPES: BEGIN OF ty_s_csv,
      mandt TYPE string,
      carrid TYPE string,
      connid TYPE string,
      text TYPE string,
      text2 TYPE string,
      END OF ty_s_csv.
      TYPES: ty_it_csv TYPE STANDARD TABLE OF ty_s_csv WITH DEFAULT KEY.
      " checkbox has the file a header
      PARAMETERS: p_header AS CHECKBOX DEFAULT 'X'.
      TRY.
      " call file open dialog
      DATA: lv_rc TYPE i,
      it_files TYPE filetable,
      lv_action TYPE i.
      cl_gui_frontend_services=>file_open_dialog( EXPORTING
      file_filter = |csv (*.csv)\|*.csv\|{ cl_gui_frontend_services=>filetype_all }|
      multiselection = abap_false
      CHANGING
      file_table = it_files
      rc = lv_rc
      user_action = lv_action ).
      IF lv_action = cl_gui_frontend_services=>action_ok.
      IF lines( it_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 #( it_files[ 1 ]-filename )
      filetype = 'ASC'
      CHANGING
      data_tab = it_csv_strings ).
      cl_demo_output=>write_data( it_csv_strings ).
      " check if header exists
      DATA(lv_start_line) = COND i( WHEN p_header = abap_true THEN 2 ELSE 1 ).
      " check if we have some 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() FROM lv_start_line.
      DATA(ls_csv_line) = VALUE ty_s_csv( ).
      SPLIT AT ';' INTO TABLE DATA(it_columns).
      IF lines( it_columns ) = 5.
      ls_csv_line-mandt = it_columns[ 1 ].
      ls_csv_line-carrid = it_columns[ 2 ].
      ls_csv_line-connid = it_columns[ 3 ].
      ls_csv_line-text = it_columns[ 4 ].
      ls_csv_line-text2 = it_columns[ 5 ].
      ENDIF.
      APPEND ls_csv_line TO it_csv.
      ENDLOOP.
      cl_demo_output=>write_data( it_csv ).
      DATA(lv_html) = cl_demo_output=>get( ).
      cl_abap_browser=>show_html( EXPORTING
      title = 'CSV Data'
      html_string = lv_html
      container = cl_gui_container=>default_screen ).
      WRITE: space.
      ENDIF.
      ENDIF.
      ENDIF.
      CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'I'.
      ENDTRY.