Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Transfering Data to Dynamic internal table

styrolution_accenture
Participant
0 Likes
2,763

Hello Expert,

Let me explain you the scenario well and need your help on this.

I am uploading data from Excel to internal table which is in the following format. In excel the no of columns is variable so we can have 3 fields or 6 fields or more.

It_excel_data[]

Rowno      Col no      Values

1               1               Record number

1               2               comp code

1               3               material number.

2               1               1

2               2               0022

2               3               Dummy Mat

Since we have the dynamic excel structure i have created a dynamic internal table using field symbols.

<FS-LINE> - Structure of this workarea and the internal table <FS-TABLE>

Record No

Comp Code

Material Number

how can we transfer the data from the it_excel_data internal table to dynamic internal table <fs-table> and it should be stored as below..

Record No

Comp Code

Material Number

10022Dummy Mat

Please suggest how to get the data in the dynamic internal table.

BR,

Rajesh Kumar.

9 REPLIES 9
Read only

former_member195270
Active Participant
0 Likes
1,633

Rajesh,

  Use following code for reading data from excel and move your data to internal table with fields Record No, Comp Code and Material Number.You are not required to use field symbols.

   DATA : lt_intern TYPE STANDARD TABLE OF  alsmex_tabline ,
           ls_intern LIKE LINE OF lt_intern.

    CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
      EXPORTING
        filename                = iv_fname
        i_begin_col             = '1'
        i_begin_row             = '2'
        i_end_col               = '3'
        i_end_row               = '99999'
      TABLES
        intern                  = lt_intern
      EXCEPTIONS
        inconsistent_parameters = 1
        upload_ole              = 2
        OTHERS                  = 3.


    LOOP AT lt_intern INTO ls_intern.
      CASE ls_intern-col.
        WHEN 1.
          ls_data-record_no = ls_intern-value.
        WHEN 2.
          ls_data-comp_code = ls_intern-value.
        WHEN 3.

          ls_data-material_code = ls_intern-value.

      ENDCASE.

      AT END OF row.
        APPEND ls_data TO t_data.
        CLEAR  ls_data.
      ENDAT.
    ENDLOOP.

Read only

0 Likes
1,633

This i know Umar.. my issue is i am working with dynamic internal table so you do not know the field name as i am using the field symbols.

As i have mentioned in my thread .. i already have the excel data in the internal table which i want to move to dynamic internal table.

Read only

0 Likes
1,633

You can move your data to dynamic table in following way.

field-symbols<fs_recno> type any,                       

                       <fs_compcode> type any,

                       <fs_matno> type any,

                       <fs_line> type any.

   assign <fs_line>* to <fs_table>.

  

assign component 'RECORD_NO' of structure  <fs_line> to  <fs_recno>.

assign component 'COMP_CODE' of structure <fs_line> to  <fs_compcode>.

assign component 'MAT_NO' of structure <fs_line> to  <fs_matno>.

LOOP AT lt_intern INTO ls_intern.

      CASE ls_intern-col.

        WHEN 1.

          <fs_recno> = ls_intern-value.         

        WHEN 2.        

           <fs_compcode> = ls_intern-value.

        WHEN 3.           

           <fs_matno> = ls_intern-value.

      ENDCASE.

       AT END OF row.
        APPEND <fs_line> TO <fs_table>.
        CLEAR  <fs_line>.
      ENDAT.

ENDLOOP.

Read only

0 Likes
1,633

"assign component 'RECORD_NO' of structure  <fs_line> to  <fs_recno>."

the above statement can not be used as we don't know the name of the 'fields while coding .. its all run time so we can not use 'RECORD_NO' .. it was just an example.

Read only

0 Likes
1,633

Hi Rajesh,

Can we try this to access the dynamic table.

lOOP AT <dyn_table> INTO <dyn_wa>.

    DO.

      ASSIGN COMPONENT  sy-index

         OF STRUCTURE <dyn_wa> TO <dyn_field>.

      IF sy-subrc <> 0.

        EXIT.

else.

**********Access your fields here <dyn_field>.**********************

      ENDIF.

    ENDDO.

  ENDLOOP.

Hope this will help.

Regards,

Amit

Read only

0 Likes
1,633

Hi,

the above statement can not be used as we don't know the name of the 'fields while coding

But apparently you have the field names in your excel sheet, so you could have created your dynamic table with the proper field names

If you didn't then you will have to work with the component numbers as already suggested... but this is a less good approach... (e.g. What about if 2 fields are switched, or new fields added?). By using the fields name, you will never have to modify your code (if your excel is altered).

Cheers,

Manu.

Read only

rosenberg_eitan
Active Contributor
0 Likes
1,633

Hi,

I had the similar requirement in the past.

At the first line of the EXCEL file We ask the user to put the target field name.


We reach an agreement with the user on a permissible field names which I check (they are in format tablename-fieldname).

Then I load the EXCEL into fixed size table(You just need to create it big enough):

TYPES: BEGIN OF tp_bffr_2 .
TYPES: value_01    TYPE alsmex_tabline-value ,
       value_02    TYPE alsmex_tabline-value ,
       value_03    TYPE alsmex_tabline-value ,
       value_04    TYPE alsmex_tabline-value ,
       .
       .
       value_32    TYPE alsmex_tabline-value .
TYPES: END OF tp_bffr_2 .
*----------------------------------------------------------------------*
TYPES: BEGIN OF tp_alv_data_1 .

INCLUDE TYPE tp_bffr_2 AS bffr_2 RENAMING WITH SUFFIX _bffr_2 .

TYPES: END OF tp_alv_data_1 .
*
TYPES: tp_alv_data_1_tab TYPE TABLE OF tp_alv_data_1 .

DATA: it_alv_data_1 TYPE tp_alv_data_1_tab .
*----------------------------------------------------------------------*

The first line in it_alv_data_1 was used as a directive where the field should be used.

Read only

RaymondGiuseppi
Active Contributor
0 Likes
1,633

Sort the returned table by row, column, at new row create a new record in internal table assigning a field symbol, and for each record use a ASSIGN COMPONENT n of the structure referenced by the previous field-symbol, where n is an integer (like column number) and not a field name, syntax to assign the nth field of the structure.

Regards,

Raymond

Read only

Former Member
0 Likes
1,633

Hi Rajesh,

You can try the following code to insert the excel data to the dynamic internal table..

LOOP AT it_excel_data INTO lwa_excel_data.

    DO.

      ASSIGN COMPONENT  sy-index

         OF STRUCTURE <fs_line> TO <fs_wa>      " declare <fs_wa>  generic field symbol

      IF sy-subrc <> 0.

        EXIT.

     ELSE.

          <fs_wa> = lwa_excel_data-values.

     ENDIF.

     AT LAST lwa_excel_data-rowno.

          APPEND <fs_line> TO <fs_table>.

    ENDDO.

  ENDLOOP.

Thanks & Regards,

Sameer Mehta