2012 Dec 30 10:56 AM
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 |
| 1 | 0022 | Dummy Mat |
Please suggest how to get the data in the dynamic internal table.
BR,
Rajesh Kumar.
2012 Dec 30 11:29 AM
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.
2012 Dec 30 12:32 PM
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.
2012 Dec 30 12:45 PM
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.
2012 Dec 30 1:16 PM
"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.
2012 Dec 30 1:38 PM
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
2012 Dec 31 10:26 AM
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.
2012 Dec 30 1:02 PM
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.
2012 Dec 30 1:26 PM
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
2012 Dec 30 3:07 PM
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