Hello community,

Tip: How To Upload CSV Files via ABAP Easily: files with comma separated values (CSV) are a good and easy way for exchanging data. Also you have the possibility to manipulate the data e.g. via Microsoft Access, Microsoft Excel or Open Office Calc easily. But for the last step, to upload the file in an SAP system, I can’t find an easy way. So I create a class with three methods to upload a CSV file. Here a code example how to do that:

"-Begin----------------------------------------------------------------- Report  Z_CSV_LOAD.   "-Variables-----------------------------------------------------------     Data CSV Type Ref To Z_CSV_IMPORT.   "-Main----------------------------------------------------------------     Delete From SFLIGHT.     Commit Work.     Create Object CSV.     CSV->Load_CSV_File( 'C:Dummysflight.csv' ).     CSV->Transform_CSV_Data( 'SFLIGHT' ).     CSV->Get_CSV_Data( 'SFLIGHT' ). "-End-------------------------------------------------------------------

With three lines of code you can upload the content of a CSV file into an SAP table.

  1. Load_CSV_File
    Loads the CSV file from the presentation server.
  2. Transform_CSV_Data
    With this method you have the possibility to manipulate the head line and the CSV data. Often it is necessary to add the field MANDT and the correct content. Also it is often necessary to correct date fields.
  3. Get_CSV_Data
    Copies the data into the SAP table. It is not necessary that the column of the CSV file must have the same position as the field in the SAP table. It is only necessary that the column and field names are equal.
"-Begin----------------------------------------------------------------- Class Z_CSV_IMPORT Definition Public Final Create Public . Public Section.   Methods Constructor.   Methods LOAD_CSV_FILE     Importing       Value(I_FILENAME) Type String.   Methods TRANSFORM_CSV_DATA     Importing       Value(I_TABLENAME) Type String.   Methods GET_CSV_DATA     Importing       Value(I_TABLENAME) Type String. Protected Section.   Data G_CSV_DATA type STRINGTAB .   Data G_ERROR_FLAG type ABAP_BOOL value ABAP_FALSE.   Data G_TAB_FIELDS Type Standard Table Of String . Private Section. EndClass. Class Z_CSV_IMPORT Implementation. Method Constructor. "-Begin-----------------------------------------------------------------   Clear G_CSV_DATA. "-End------------------------------------------------------------------- EndMethod. Method GET_CSV_DATA. "-Begin-----------------------------------------------------------------   "-Structures----------------------------------------------------------     Types: Begin Of l_typ_Confrontation,              IntFieldName Type String,              IntFieldPos Type i,              IntFieldTyp Type String,              CSVFieldPos Type i,              CSVFieldName Type String,            End Of l_typ_Confrontation.   "-Variables-----------------------------------------------------------     Data l_rda_data Type Ref To Data.     Data l_rda_wa Type Ref To Data.     Data l_rcl_descr_tab Type Ref To cl_abap_tabledescr.     Data l_rcl_descr_struc Type Ref To cl_abap_structdescr.     Data l_comp_descr Type abap_compdescr.     Data l_tab_content Type Standard Table Of String.     Data l_Line Type String Value ''.     Data l_tab_confrontation Type Standard Table Of l_typ_Confrontation       With Key CSVFieldPos.     Data l_FieldName Type String Value ''.     Data l_Content Type String Value ''.     Data l_Conf Type l_typ_Confrontation.     Field-Symbols   Type Standard Table.     Field-Symbols   Type Any.     Field-Symbols   Type Any.   "-Main----------------------------------------------------------------     If G_CSV_DATA Is Not Initial And G_ERROR_FLAG = ABAP_FALSE.       "-Reference to Table----------------------------------------------         Create Data l_rda_data Type Standard Table Of (I_TABLENAME).         Assign l_rda_data- >* To  .       "-Get Structure of Table------------------------------------------         l_rcl_descr_tab ?= cl_abap_typedescr= >describe_by_data(   ).         l_rcl_descr_struc ?= l_rcl_descr_tab- >get_table_line_type( ).       "-Define Line of Table--------------------------------------------         Create Data l_rda_wa Like Line Of  .         Assign l_rda_wa- >* To  .       "-Compare Field Names of the Table with Headline of CSV-----------       "-       "- With this step is the position of the column indifferent. It       "- is only necessary that the field of the table and the column       "- of the CSV file must have the same name.       "-       "-----------------------------------------------------------------         Loop At l_rcl_descr_struc- >components Into l_comp_descr.           l_Conf-INTFIELDNAME = l_comp_descr-NAME.           l_Conf-INTFIELDPOS = sy-tabix.           l_Conf-INTFIELDTYP = l_comp_descr-TYPE_KIND.           Loop At g_tab_fields Into l_FieldName.             l_Conf-CSVFIELDPOS = -1.             l_Conf-CSVFIELDNAME = 'UNKNOWN'.             If l_comp_descr-NAME = l_FieldName.               l_Conf-CSVFIELDNAME = l_FieldName.               l_Conf-CSVFIELDPOS = sy-tabix.               Exit.             EndIf.           EndLoop.           Append l_Conf To l_tab_confrontation.         EndLoop.         Delete l_tab_confrontation Where CSVFIELDPOS = -1.         Sort l_tab_confrontation By CSVFIELDPOS.       "-Copy Data-------------------------------------------------------         Loop At G_CSV_DATA Into l_Line From 2.           Split l_Line At ';' Into Table l_tab_content.           Loop At l_tab_content Into l_Content.             Condense l_Content.             Read Table l_tab_confrontation With Key CSVFieldPos = sy-tabix               Into l_Conf.             If sy-subrc = 0.               Assign Component l_Conf-INTFIELDNAME Of Structure                   To  .               If l_Conf-INTFIELDTYP = 'P'.                 Replace All Occurrences Of '.' In l_Content With ''.                 Replace ',' In l_Content With '.'.                   = l_Content.               Else.                   = l_Content.               EndIf.             EndIf.           EndLoop.           Append   To  .           Clear  .         EndLoop.       "-Write Data into Table-------------------------------------------         Insert (I_TABLENAME) From Table  .         If sy-subrc  <> 0.           G_ERROR_FLAG = ABAP_TRUE.         EndIf.     EndIf. "-End------------------------------------------------------------------- EndMethod. Method LOAD_CSV_FILE. "-Begin-----------------------------------------------------------------   Call Function 'GUI_UPLOAD'     Exporting       FILENAME = i_FileName       FILETYPE = 'ASC'     Tables       DATA_TAB = g_csv_data     Exceptions      OTHERS = 1.    If sy-subrc  <> 0.      G_ERROR_FLAG = ABAP_TRUE.    EndIf. "-End------------------------------------------------------------------- EndMethod. Method TRANSFORM_CSV_DATA. "-Begin-----------------------------------------------------------------   "-Variables-----------------------------------------------------------     Data l_Fld1 Type String Value ''.     Data l_Fld2 Type String Value ''.     Data l_Fld3 Type String Value ''.     Data l_FldRest Type String Value ''.     Field-Symbols   Type String.   "-Main----------------------------------------------------------------     If G_CSV_DATA Is Not Initial And G_ERROR_FLAG = ABAP_FALSE.       "-Manipulate Headline---------------------------------------------         Read Table G_CSV_DATA Index 1 Assigning  .           = 'MANDT;' &&  .       Condense   No-Gaps.       Split   At ';' Into Table g_tab_fields.     EndIf.     "-Transformation----------------------------------------------------       Loop At G_CSV_DATA From 2 Assigning  .           = sy-mandt && ';' &&  .       EndLoop. "-End------------------------------------------------------------------- EndMethod. EndClass. "-End-------------------------------------------------------------------

Enjoy it.

Cheers

Stefan

New NetWeaver Information at SAP.com

Very Helpfull

 

 

User Rating: Be the first one !