Hi All,

To perform mass operations, one of the most popular method is to upload an excel or csv file. Although we have GUI_UPLOAD function module to upload a file and get the data in the internal table but this is limited to SAP GUI only. In cases where the file is getting uploaded from UI5 front end, we get xstring file in the back end. We need to parse this file and extract data from it. There is some limitations in terms of converting the data of excel file(xstring) into internal table (explored on SCN and found out that we cannot easily convert the xstring of .xls into internal table while its easy in case of .csv) so we are restricted to use CSV files.

The scope of this blog does not contain the process of file uploading from UI5 front end and receiving it in the Gateway service. This part you can easily achieve using by these references:

1) Gateway service part: How To Upload and Download Files Using SAP NW Gateway SP06

2) File Uploading part: Uploading files to SAP using HTML5 /AJAX/Gateway media links with real-time progress bar

I am gonna cover only the conversion part of that csv to any internal table using dynamic internal table concept. Once you get the data in your internal table you can use it in any way.

Once you get the xstring of csv file in back-end, you can use the below FM to extract data. The input and output parameters are as below:

Import.PNG

table.PNG

In input parameters, you need to pass remove_header flag as ‘X’ if the first line of your csv file contains header field names. In tables, you can pass any internal table for getting the data. I am using dynamic internal table for this.

The code is:


FUNCTION zui5_csv_to_itab.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(FILE_CONTENT) TYPE  XSTRINGVAL OPTIONAL
*"     VALUE(REMOVE_HEADER) TYPE  CHAR1 DEFAULT 'X'
*"  TABLES
*"      DATA_TABLE OPTIONAL
*"----------------------------------------------------------------------
   DATA struct_descr TYPE REF TO cl_abap_structdescr.
   FIELD-SYMBOLS:
     <comp_descr> LIKE LINE OF cl_abap_structdescr=>components,
     <comp> TYPE any.
   DATA: ls_string TYPE string,
          it_line TYPE TABLE OF string,
          wa_line TYPE string,
          it_line2 TYPE TABLE OF string,
          wa_line2 TYPE string,
          l_regex TYPE string.
   DATA : l_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.
   CALL FUNCTION 'HR_KR_XSTRING_TO_STRING'
     EXPORTING
*     FROM_CODEPAGE = '8500'
       in_xstring    = file_content
*     OUT_LEN       =
     IMPORTING
       out_string    = ls_string.
   SPLIT ls_string AT cl_abap_char_utilities=>cr_lf INTO TABLE it_line.
   TRY.
       struct_descr ?= cl_abap_typedescr=>describe_by_data( data_table ).
     CATCH cx_sy_move_cast_error.
       RETURN.
   ENDTRY.
   LOOP AT it_line INTO wa_line.
     IF sy-index = 1 AND remove_header = 'X'.
     ELSE.
       REPLACE ALL OCCURRENCES OF
       REGEX ',(?=(?:[^"]*$)|(?:[^"]*"[^"]*"[^"]*)*$)'
       IN wa_line WITH l_tab.
       REPLACE FIRST OCCURRENCE OF
         REGEX '^"'
         IN wa_line WITH ' '.
       CONCATENATE '"' l_tab '"|"' l_tab '|' l_tab '"' INTO l_regex.
       REPLACE ALL OCCURRENCES OF
         REGEX l_regex                         "    '"l_tab"|"l_tab|l_tab"'
         IN wa_line WITH l_tab.
       REPLACE ALL OCCURRENCES OF '""' IN wa_line
         WITH '"'.    "Added during testing
       REPLACE ALL OCCURRENCES OF REGEX '"$' IN wa_line
         WITH ' '.
       SPLIT wa_line AT l_tab INTO TABLE it_line2.
       LOOP AT struct_descr->components ASSIGNING <comp_descr>.
         ASSIGN COMPONENT <comp_descr>-name
                OF STRUCTURE data_table TO <comp>.
         READ TABLE it_line2 INTO <comp> INDEX sy-tabix.
       ENDLOOP.
       APPEND data_table TO data_table.
       REFRESH it_line2.
     ENDIF.
   ENDLOOP.
ENDFUNCTION.


Reference: ABAP RegEx met CSV and said ‘I’ll keep a Tab on you’ | &amp;#169; Passionate about SAP – A Blog

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply