Copyright 2024 - BV TallVision IT

CSV files are golden: they are the simplest form to pass data and every system can understand it. The first line holds the column names, data lines hold the field contents. As they are so common, I've devised a small local class that makes handling them a bit easier. Local class lcl_CSV_utility:

The utility is in fact a string manipulation utility. Read the file and pass the header line to the utility. Then pass the data line and start reading fields or altering content of a field. The coding:

class lcl_csv_utility DEFINITION.

  public section.

    data: gv_SUBRC type sy-subrc READ-ONLY,
          gt_header_tokens type standard table of char30 READ-ONLY,
          gt_data_tokens type standard table of string,
          gv_separator type c length 1. 

    methods: constructor IMPORTING separator TYPE any DEFAULT ';',
             set_header importing headerline type string,
             set_data importing dataline type string,
             get_data importing header type boolean default abap_false
               returning value(dataline) type string,
             get importing fieldId type any dataline type string default space
               returning value(fieldvalue) type string,
             set importing fieldId type any
               fieldvalue type string
               dataline type string default space.

  private section.
    methods: get_tabix importing fieldId type any
               returning value(tabix) type sy-tabix.

endclass.

class lcl_csv_utility IMPLEMENTATION.
  method constructor.
    gv_separator = separator.
  endmethod.
  method set_header.
    clear: gt_header_tokens[].
    split headerline at gv_separator into table gt_header_tokens.
  endmethod.
  method set_data.
    clear: gt_data_tokens[].
    split dataline at gv_separator into table gt_data_tokens.
  endmethod.
  method get_data.
    data: lv_token type string.
    clear dataline.
    if header = abap_true.
      loop at gt_header_tokens into lv_token.
        concatenate dataline lv_token into dataline SEPARATED BY gv_separator.
      endloop.
    else.
      loop at gt_data_tokens into lv_token.
        concatenate dataline lv_token into dataline SEPARATED BY gv_separator.
      endloop.
    endif.
    shift dataline left by 1 places.
  endmethod.
  method get_tabix.
    data: lv_fieldtype type c length 1.

    clear: tabix, gv_SUBRC.
    describe field fieldId type lv_fieldtype.
    case lv_fieldtype.
      when 'C'.
        read table gt_header_tokens with key table_line = fieldid TRANSPORTING NO FIELDS.
        if sy-subrc <> 0.
          gv_SUBRC = 4.
        else.
          tabix = sy-tabix.
        endif.
      when 'I'.
        tabix = fieldId.
      when others.
        gv_SUBRC = 4.
    endcase.
  endmethod.
  method get.
    data: lv_tabix type sy-tabix.

    clear: gv_SUBRC.
    if dataline is SUPPLIED.
      set_data( dataline ).
    endif.
    lv_tabix = get_tabix( fieldId ).
    if gv_SUBRC = 0.
      read table gt_data_tokens index lv_tabix into fieldvalue.
      if sy-subrc <> 0.
        gv_SUBRC = 5.
        clear fieldvalue.
      endif.
    endif.
  endmethod.
  method set.
    data: lv_tabix type sy-tabix,
          lv_tsize type sy-tabix.

    clear: gv_SUBRC.
    if dataline is SUPPLIED.
      set_data( dataline ).
    endif.
    lv_tabix = get_tabix( fieldId ).
    if gv_SUBRC = 0.
      describe table gt_data_tokens lines lv_tsize.
      if lv_tabix between 1 and lv_tsize.
        modify gt_data_tokens index lv_tabix from fieldvalue.
        if sy-subrc <> 0.
          gv_SUBRC = 5.
        endif.
        gv_SUBRC = 9.
      endif.
    endif.
  endmethod.

endclass.

A small demonstration of how this is used:

  data: go_csv type ref to lcl_csv_utility,
        lv_result type string.

  CREATE OBJECT go_csv.
  go_csv->set_header( 'PERNR;MATNR;MAKTX;VELD23' ).
  go_scv->set_data( '123;MAT2412;Wonderbaarlijke vervolg;N12' ). 
  lv_result = go_csv->get( fieldId = 'MAKTX' ).
  write: / lv_result.
  lv_result = go_csv->get( fieldId = 2 ).
  write: / lv_result.
  lv_result = go_csv->get( fieldId = 7 ).
  write: / lv_result, go_csv->gv_subrc.

  lv_result = go_csv->get_data( header = abap_true ).
  write: / lv_result.
  lv_result = go_csv->get_data( ).
  write: / lv_result.
  go_csv->set( fieldId = 4 fieldvalue = 'Kaas' ).
  lv_result = go_csv->get_data( ).
  write: / lv_result.

The above will yield the following output:

Wonderbaarlijke vervolg
MAT2412
     5
PERNR;MATNR;MAKTX;VELD23
123;MAT2412;Wonderbaarlijke vervolg;N12
123;MAT2412;Wonderbaarlijke vervolg;Kaas