11-14-2006 4:37 PM
Hi,
I have CSV file
a;b;c;d;e;f;g;
as;sd;fg;gh;jk;kl;mn;;;;;;;;;
qw;we;rt;yu;ui;io;;;;;;;;;;;;;;
which has to move to itab as following format,so each row has to be dynamic
a b c d e f g
as sd df fg gh jk kl mn
qw we rt yu ui io
above file each row can vary means later file can be like
a;b;c;d;e;f;g;h;i
as;sd;fg;gh;jk;kl;mn;;;;;;;;;
qw;we;rt;yu;ui;io;pq;;;;;;;;;;;;;
Now ,which has to move to itab as following format,so each row has to be dynamic
a b c d e f g h i
as sd df fg gh jk kl mn
qw we rt yu ui io pq
Please let me know how to handle this situtaion.I am very sure that
we have to Split the row by ; but dynamically and need to move to Dynamic internal
table.
Any suggestion and idea on this would be appreciable.
Thanks
-Pramod
11-14-2006 5:28 PM
Assuming your delimited data is in table gt_data, this should populate <re_table> the way you would like.
DATA: BEGIN OF gt_data OCCURS 0,
line(200),
END OF gt_data.
DATA: BEGIN OF gt_fields OCCURS 0,
field(50),
END OF gt_fields.
DATA: BEGIN OF gt_matrix OCCURS 0,
cnt(2) TYPE n,
value(50),
END OF gt_matrix.
DATA: g_field(10),
g_index(2) TYPE n,
g_lines TYPE i,
g_max_lines TYPE i.
* ALV TYPES & DATA
TYPE-POOLS: slis.
DATA: t_alv_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
s_alv_layout TYPE slis_layout_alv,
p_alv TYPE slis_vari VALUE '/DEFAULT',
t_alv_events TYPE slis_t_event.
DATA: s_fcat TYPE lvc_s_fcat,
t_fcat TYPE lvc_t_fcat,
re_table TYPE REF TO data,
re_head TYPE REF TO data.
FIELD-SYMBOLS: <re_table> TYPE table,
<re_head>,
<re_field>.
* Determine number of columns required and store data in row/column
* pairings
LOOP AT gt_data.
SPLIT gt_data AT ';' INTO TABLE gt_fields.
DESCRIBE TABLE gt_fields LINES g_lines.
IF g_lines > g_max_lines.
g_max_lines = g_lines.
ENDIF.
MOVE sy-tabix TO gt_matrix-cnt.
LOOP AT gt_fields.
MOVE gt_fields-field TO gt_matrix-value.
APPEND gt_matrix.
ENDLOOP.
ENDLOOP.
* Define field catalog
DO g_max_lines TIMES.
MOVE sy-index TO g_index.
CONCATENATE 'COL' g_index INTO s_fcat-fieldname.
MOVE: sy-index TO s_fcat-col_pos,
'C' TO s_fcat-inttype,
'10' TO s_fcat-intlen.
APPEND s_fcat TO t_fcat.
ENDDO.
* Build dynamic internal table
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = t_fcat
IMPORTING
ep_table = re_table.
ASSIGN re_table->* TO <re_table>.
CREATE DATA re_head LIKE LINE OF <re_table>.
ASSIGN re_head->* TO <re_head>.
* Populate dynamic table
CLEAR g_index.
LOOP AT gt_matrix.
ADD 1 TO g_index.
CONCATENATE 'COL' g_index INTO g_field.
ASSIGN COMPONENT g_field OF STRUCTURE <re_head> TO <re_field>.
MOVE gt_matrix-value TO <re_field>.
AT END OF cnt.
APPEND <re_head> TO <re_table>.
CLEAR g_index.
ENDAT.
ENDLOOP.
11-14-2006 5:28 PM
Assuming your delimited data is in table gt_data, this should populate <re_table> the way you would like.
DATA: BEGIN OF gt_data OCCURS 0,
line(200),
END OF gt_data.
DATA: BEGIN OF gt_fields OCCURS 0,
field(50),
END OF gt_fields.
DATA: BEGIN OF gt_matrix OCCURS 0,
cnt(2) TYPE n,
value(50),
END OF gt_matrix.
DATA: g_field(10),
g_index(2) TYPE n,
g_lines TYPE i,
g_max_lines TYPE i.
* ALV TYPES & DATA
TYPE-POOLS: slis.
DATA: t_alv_fieldcat TYPE slis_t_fieldcat_alv WITH HEADER LINE,
s_alv_layout TYPE slis_layout_alv,
p_alv TYPE slis_vari VALUE '/DEFAULT',
t_alv_events TYPE slis_t_event.
DATA: s_fcat TYPE lvc_s_fcat,
t_fcat TYPE lvc_t_fcat,
re_table TYPE REF TO data,
re_head TYPE REF TO data.
FIELD-SYMBOLS: <re_table> TYPE table,
<re_head>,
<re_field>.
* Determine number of columns required and store data in row/column
* pairings
LOOP AT gt_data.
SPLIT gt_data AT ';' INTO TABLE gt_fields.
DESCRIBE TABLE gt_fields LINES g_lines.
IF g_lines > g_max_lines.
g_max_lines = g_lines.
ENDIF.
MOVE sy-tabix TO gt_matrix-cnt.
LOOP AT gt_fields.
MOVE gt_fields-field TO gt_matrix-value.
APPEND gt_matrix.
ENDLOOP.
ENDLOOP.
* Define field catalog
DO g_max_lines TIMES.
MOVE sy-index TO g_index.
CONCATENATE 'COL' g_index INTO s_fcat-fieldname.
MOVE: sy-index TO s_fcat-col_pos,
'C' TO s_fcat-inttype,
'10' TO s_fcat-intlen.
APPEND s_fcat TO t_fcat.
ENDDO.
* Build dynamic internal table
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = t_fcat
IMPORTING
ep_table = re_table.
ASSIGN re_table->* TO <re_table>.
CREATE DATA re_head LIKE LINE OF <re_table>.
ASSIGN re_head->* TO <re_head>.
* Populate dynamic table
CLEAR g_index.
LOOP AT gt_matrix.
ADD 1 TO g_index.
CONCATENATE 'COL' g_index INTO g_field.
ASSIGN COMPONENT g_field OF STRUCTURE <re_head> TO <re_field>.
MOVE gt_matrix-value TO <re_field>.
AT END OF cnt.
APPEND <re_head> TO <re_table>.
CLEAR g_index.
ENDAT.
ENDLOOP.
11-14-2006 5:35 PM
Looks like Michael and I were thinking the same. You need to find the biggest record length first, then build your dynamica internal table, then fill it with data.
report zrich_0001.
types: begin of ttab,
rec(1000) type c,
end of ttab.
types: begin of tdat,
fld1(10) type c,
fld2(10) type c,
fld3(10) type c,
end of tdat.
data: itab type table of ttab with header line.
data: idat type table of tdat with header line.
type-pools : abap, slis.
field-symbols: <dyn_table> type standard table,
<dyn_wa>,
<dyn_field>.
data: new_table type ref to data,
new_line type ref to data,
wa_it_fldcat type lvc_s_fcat,
it_fldcat type lvc_t_fcat..
data: file_str type string.
data: istr type table of string with header line.
data: istr_lines type i,
biggest_string type i,
index(4) type c.
parameters: p_file type localfile.
at selection-screen on value-request for p_file.
call function 'KD_GET_FILENAME_ON_F4'
exporting
static = 'X'
changing
file_name = p_file.
start-of-selection.
file_str = p_file.
call function 'GUI_UPLOAD'
exporting
filename = file_str
tables
data_tab = itab
exceptions
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
others = 17.
* What is the biggest string?
loop at itab.
split itab-rec at ';' into table istr.
describe table istr lines istr_lines.
if biggest_string < istr_lines.
biggest_string = istr_lines.
endif.
endloop.
* build fieldcat per how many fields are needed from the biggest string.
do biggest_string times.
clear wa_it_fldcat.
index = sy-index.
concatenate 'FLD' index into wa_it_fldcat-fieldname.
condense wa_it_fldcat-fieldname no-gaps.
wa_it_fldcat-datatype = 'C'.
wa_it_fldcat-inttype = 'C'.
wa_it_fldcat-intlen = '10'.
append wa_it_fldcat to it_fldcat .
enddo.
* Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = it_fldcat
importing
ep_table = new_table.
assign new_table->* to <dyn_table>.
* Create dynamic work area and assign to FS
create data new_line like line of <dyn_table>.
assign new_line->* to <dyn_wa>.
* Now fill dynamic internal table.
loop at itab.
clear istr. refresh istr.
split itab at ';' into table istr.
clear <dyn_wa>.
loop at istr.
assign component sy-tabix of structure <dyn_wa> to <dyn_field>.
if sy-subrc <> 0.
exit.
endif.
if sy-subrc = 0 and not istr is initial.
<dyn_field> = istr.
endif.
at last.
append <dyn_wa> to <dyn_table>.
endat.
endloop.
endloop.
* Write out data from 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.
endif.
if sy-index = 1.
write:/ <dyn_field>.
else.
write: <dyn_field>.
endif.
enddo.
endloop.
My file looks like this.
A;b;c;d;e
f;g;
h;i;j;;k;l;m;n;o;p;
q;r;s;t;u
v;
This code worked pretty good for me.
Regards,
Rich Heilman