
"-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:\Dummy\sflight.csv' ).
CSV->Transform_CSV_Data( 'SFLIGHT' ).
CSV->Get_CSV_Data( 'SFLIGHT' ).
"-End-------------------------------------------------------------------
"-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 <l_Table> Type Standard Table.
Field-Symbols <l_comp> Type Any.
Field-Symbols <l_wa> 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 <l_Table>.
"-Get Structure of Table------------------------------------------
l_rcl_descr_tab ?= cl_abap_typedescr= >describe_by_data( <l_Table> ).
l_rcl_descr_struc ?= l_rcl_descr_tab- >get_table_line_type( ).
"-Define Line of Table--------------------------------------------
Create Data l_rda_wa Like Line Of <l_Table>.
Assign l_rda_wa- >* To <l_wa>.
"-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 <l_wa>
To <l_comp>.
If l_Conf-INTFIELDTYP = 'P'.
Replace All Occurrences Of '.' In l_Content With ''.
Replace ',' In l_Content With '.'.
<l_comp> = l_Content.
Else.
<l_comp> = l_Content.
EndIf.
EndIf.
EndLoop.
Append <l_wa> To <l_Table>.
Clear <l_wa>.
EndLoop.
"-Write Data into Table-------------------------------------------
Insert (I_TABLENAME) From Table <l_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 <Line> 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 <Line>.
<Line> = 'MANDT;' && <Line>.
Condense <Line> No-Gaps.
Split <Line> At ';' Into Table g_tab_fields.
EndIf.
"-Transformation----------------------------------------------------
Loop At G_CSV_DATA From 2 Assigning <Line>.
<Line> = sy-mandt && ';' && <Line>.
EndLoop.
"-End-------------------------------------------------------------------
EndMethod.
EndClass.
"-End-------------------------------------------------------------------
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |