Friday, 22 August 2014

CSV Files with Commas in the fields....

Okay, so the initial requirement was to load a CSV file in to SAP and process the content. This all seemed fine, and I processed it as follows:

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename                      = v_filename_string
       filetype                      = 'DAT'
    TABLES
      data_tab                      = itab.

    loop at itab.
      split itab-text at ',' into w_input-matnr w_input-desc w_input-date w_input-demand.
      append w_input to t_input.
   endloop.

But it became apparent that the users, saving data in Excel CSV, wanted to include commas in the field values (specifically the w_input-desc). Obviously the split just takes these as delimiters and messes up the demand and date values.

To get round this, instead of “GUI Upload”, we’re going to use


  CALL FUNCTION 'KCD_CSV_FILE_TO_INTERN_CONVERT'
    EXPORTING
      i_filename      = l_filename
      i_separator     = ','
    TABLES
      e_intern        = lt_csv
    EXCEPTIONS
      upload_csv      = 1
      upload_filetype = 2
      OTHERS          = 3.

This results in a slightly awkward table :

     Row  Col  Value
1    0001 0001 40253
2    0001 0002 Crispy Aromatic, Half Duck 20x32
3    0001 0003 14:32
4    0001 0004 40
5    0002 0001 40253
6    0002 0002 Crispy Aromatic, Half Duck 20x32
7    0002 0003 14:33
8    0002 0004 50


But I can loop round this, and create my t_input table from there…
"Tee up the t_input table.

lt_csv_bluff[] lt_csv[].
delete ADJACENT DUPLICATES FROM lt_csv_bluff COMPARING row.

loop at lt_csv_bluff into lv_csv_bluff.
  append w_input to t_input.
endloop.


loop at lt_csv into lv_csv.
  assign COMPONENT lv_csv-col of STRUCTURE w_input to <fs>.
  <fs> lv_csv-value.
  MODIFY t_input from w_input INDEX lv_csv-row.
endloop.


No comments:

Post a Comment