‎2006 Nov 07 2:45 PM
Hi,
I have to import a file CSV using ';' as field separator.
I'm tryng to use Upload function but it doesn't work (it need "tab" as separator).
Any solution?
Thanks
Salvatore
‎2006 Nov 07 2:57 PM
Hi, in order to upload a comma delimited file, you need to upload to a flat structure, then parse it out using the SPLIT statement, here is a sample.
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.
data: file_str type string.
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.
loop at itab.
clear idat.
split itab-rec at ',' into idat-fld1
idat-fld2
idat-fld3.
append idat.
endloop.
loop at idat.
write:/ idat-fld1, idat-fld2, idat-fld3.
endloop.
Regards,
Rich Heilman
‎2006 Nov 07 2:48 PM
I believe you will have to load it using a standard upload function (like GUI_UPLOAD) into a generic internal table, then loop through the table using the SPLIT function to separate the data into the respective fields.
‎2006 Nov 07 2:53 PM
Hi,
Refer Sample code:
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = lcl_filename
filetype = 'ASC'
<b>has_field_separator = 'X'</b>
TABLES
data_tab = upload_table
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.
Then Loop at <b>upload_table</b> and populate into corresponding fields of work area and apend into final internal Tabel.
Reward points if this helps.
Manish
‎2006 Nov 07 3:52 PM
Sorry Manish Kumar... same result than using UPLOAD because I can't choose the seprator field.
Message was edited by: Salvatore Volpe
‎2006 Nov 07 3:53 PM
‎2006 Nov 17 2:12 PM
Hi,
is possible to substitute this loop with the attached one:
////////////////////////////////////////// OLD
loop at itab.
clear idat.
split itab-rec at ',' into idat-fld1
idat-fld2
idat-fld3.
append idat.
endloop.
//////////////////////////////////////////////////////// NEW
// suppose Itab and idat with header line.
DATA: line TYPE string.
DATA: n TYPE i VALUE 0.
FIELD-SYMBOLS <f>. " type string.
DATA: buff(256) TYPE n.
DATA: len TYPE i.
LOOP AT itab.
CLEAR idat.
DO.
CLEAR buff.
ADD 1 TO n.
ASSIGN COMPONENT n OF STRUCTURE idat TO <f>.
IF sy-subrc EQ 0.
SPLIT itab AT ';' INTO <f>
itab.
ELSE.
EXIT.
ENDIF.
ENDDO.
APPEND idat.
N = 0.
ENDLOOP.
using this code you don't need to change program is idat structure changes.
Saluti
Salvatore
Message was edited by:
Salvatore Volpe
‎2006 Nov 07 2:57 PM
Hi, in order to upload a comma delimited file, you need to upload to a flat structure, then parse it out using the SPLIT statement, here is a sample.
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.
data: file_str type string.
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.
loop at itab.
clear idat.
split itab-rec at ',' into idat-fld1
idat-fld2
idat-fld3.
append idat.
endloop.
loop at idat.
write:/ idat-fld1, idat-fld2, idat-fld3.
endloop.
Regards,
Rich Heilman