A Generic program to upload data into a ztable: Common Approach

  1. Create a local type structure or DDIC structure to match the columns of the excel sheet.
  2. Crate an internal table of the DDIC structure.
  3. Use FM TEXT_CONVERT_XLS_TO_SAP to read data from excel into internal table
  4. Now the entire excel data is internal table which allows us to perform validation, map the internal table data to a ZTABLE structure and update the data into ztable.

The only disadvantage is that the program is not generic and it is specific to that ztable and the excel format.  However, a cleaner approach would be to Use RTTS. The advantage of using RTTS in this case is:

  1. The program can be used to load data to any ztable. It is not specific to any ztable.
  2. The program need not be changed even if a new field is added or removed from the ztable.
  3. We do not need change the program even if column is added or removed from the source excel file.  The source structure and internal table is created dynamically.

Simple Selection Screen with two parameters in it.

*parameter for table name and file path.
PARAMETERS : p_tabnam TYPE tabname16,
p_filep
TYPE rlgrapfilename.

Declaration :

*required type pool

TYPE-POOLS abap .

*variables to store the structure of the table

DATA : lit_component_table TYPE  cl_abap_structdescr=>component_table,
lwa_component_table
LIKE LINE OF lit_component_table.

 

* reference variables to structure and table class

 

DATA lref_struct TYPE REF TO cl_abap_structdescr,
lref_table
TYPE REF TO cl_abap_tabledescr.

*variables to be create dynamically

DATA : lit_table TYPE REF TO data.

*field symbol to store data

FIELD-SYMBOLS : TYPE table.

DATA : lv_message TYPE string.

START-OF-SELECTION

*we will use the describe by name method and get components to find the list of fields in the table.

lref_struct ?= cl_abap_structdescr=>describe_by_name( p_name = p_tabnam ).

lit_component_table = lref_struct->get_components( ).

TRY.
*create a new structure based on the fields. If required new columns can be added/removed to this table.
lref_struct
= cl_abap_structdescr=>create(
p_components
= lit_component_table
).

* create a new internal table based on the structure created.

lref_table = cl_abap_tabledescr=>create(
p_line_type 
= lref_struct
*    p_table_kind = TABLEKIND_STD
*    p_unique     = ABAP_FALSE
*    p_key        =
*    p_key_kind   = KEYDEFKIND_DEFAULT
).
*
CREATE DATA lit_table TYPE HANDLE lref_table.


ASSIGN lit_table->* TO .

CATCH cx_sy_table_creation INTO lref_ex_table. “Exception when Creating a Table Type
lv_message
= lref_ex_table->get_text( ).

ENDTRY.

*using this field symbol we will call the below method to read data from excel. If needed we can use other FM’s or method to read data from excel.

 

IF lv_message IS INITIAL AND IS ASSIGNED.

CALL FUNCTION ‘TEXT_CONVERT_XLS_TO_SAP’
EXPORTING
*     I_FIELD_SEPERATOR          =
i_line_header             
= ‘X’
i_tab_raw_data            
= lv_text_data
i_filename                
= p_filep
TABLES
i_tab_converted_data      
=
*   EXCEPTIONS
*     CONVERSION_FAILED          = 1
*     OTHERS                     = 2
.
IF sysubrc <> 0.
* Implement suitable error handling here
ENDIF.

ENDIF.

*since this is a demo program we are directly modifying the data using the modify statement. In a real scenario we would need to validate, enqueue and then update the data 

 

MODIFY (p_tabnam) FROM TABLE .

IF sy-subrc eq 0.

     COMMIT WORK.

ENDIF.

New NetWeaver Information at SAP.com

Very Helpfull

 

 

User Rating: Be the first one !