01-18-2006 7:15 PM
hi all,
how can i upload a csv file containing fields with thousand separators as commas and date fields that are not in sap format using the fm gui_upload.
01-18-2006 7:19 PM
01-18-2006 7:23 PM
Hi Rich,
i've mentioned that the file is in csv format, i.e., it is a comma delimited file, isn't it?
01-18-2006 7:28 PM
Ok, here is an example of uploading a tab delimited text file. Notice here we are bringing the values to ITAB which is a generic internal table. Then looping there and converting the values to TYPE P and TYPE sy-datum into another ITAB2.
My file looks like this. There are tabs in there.
TestField1 1,654.00 01/18/2006
TestField2 1,541.00 01/19/2006
Program code.
report zrich_0004
no standard page heading.
data: begin of itab occurs 0,
fld1(20) type c,
fld2(20) type c,
fld3(20) type c,
end of itab.
data: begin of itab2 occurs 0,
fld1(20) type c,
fld2 type p decimals 2,
fld3 type sy-datum,
end of itab2.
call function 'GUI_UPLOAD'
exporting
filename = 'C:test.txt'
filetype = 'ASC'
has_field_separator = 'X'
tables
data_tab = itab.
check sy-subrc = 0.
loop at itab.
itab2-fld1 = itab-fld1.
while sy-subrc = 0.
replace ',' with space into itab-fld2 .
endwhile.
condense itab-fld2 no-gaps.
itab2-fld2 = itab-fld2.
call function 'CONVERT_DATE_TO_INTERNAL'
exporting
date_external = itab-fld3
importing
date_internal = itab2-fld3.
append itab2.
endloop.
check sy-subrc = 0.
Regards,
Rich Heilman
01-18-2006 7:29 PM
01-18-2006 7:34 PM
Here is a sample for comma delimited. Again you must not have commas where they are not column separators.
The file....
TestField1,1654.00,01/18/2006
TestField2,1541.00,01/19/2006
The program....
report zrich_0004
no standard page heading.
data: irec type table of string with header line.
data: begin of xtab,
fld1(20) type c,
fld2(20) type c,
fld3(20) type c,
end of xtab.
data: begin of itab2 occurs 0,
fld1(20) type c,
fld2 type p decimals 2,
fld3 type sy-datum,
end of itab2.
call function 'GUI_UPLOAD'
exporting
filename = 'C:test.txt'
filetype = 'ASC'
tables
data_tab = irec.
check sy-subrc = 0.
loop at irec.
split irec at ',' into xtab-fld1 xtab-fld2 xtab-fld3.
itab2-fld1 = xtab-fld1.
itab2-fld2 = xtab-fld2.
call function 'CONVERT_DATE_TO_INTERNAL'
exporting
date_external = xtab-fld3
importing
date_internal = itab2-fld3.
append itab2.
endloop.
check sy-subrc = 0.
Regards,
Rich Heilman
01-18-2006 7:33 PM
Hi Vijay,
You can upload the file into internal table.But you have to loop at the table & remove the " which will be generated for the ',' .
for example:
data:begin of itab occurs 0,
field1(10),
field2(10),
end of itab.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = 'C:\phani.csv'
FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = ' '
HEADER_LENGTH = 0
READ_BY_LINE = 'X'
DAT_MODE = ' '
CODEPAGE = ' '
IGNORE_CERR = ABAP_TRUE
REPLACEMENT = '#'
CHECK_BOM = ' '
IMPORTING
FILELENGTH =
HEADER =
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.
replace ALL OCCURRENCES OF '"' in itab-field1 with ''.
replace ALL OCCURRENCES OF '"' in itab-field2 with ''.
modify itab.
endloop.
loop at itab.
write:/ itab.
endloop.
01-19-2006 10:08 AM
Hi Phani,
even when we replace " with space the , which is present in a field of the file will still exist. it will pose some problems while splitting into different fields.
01-19-2006 10:12 AM
After you replace ',' with space try using CONDENSE.
I mean immediately after your replace statement.
Regards,
Srikanth
Message was edited by: Srikanth Lodd
01-19-2006 10:14 AM
Hi Vijay
You should know how it's your file, so the position of every field in the record.
So if T_FILE is your table with CSV file and wa is work area with all field you need
DATA: T_FIELD(100) TYPE C OCCURS 100.
LOOP AT T_FILE.
SPLIT T_FILE AT ',' INTO TABLE T_FIELD.
LOOP AT T_FIELD.
ASSIGN COMPONENT SY-TABIX OF STRUCTURE WA TO <FS>.
<FS> = T_FIELD.
ENDLOOP.
APPEND WA TO ITAB.
ENDLOOP.
Now in ITAB you have your data splitted.
Max
01-19-2006 10:14 AM
Hi Vijay,
If you use the above FM, you need not to split or you need not replace. directly data will go to itab fields.
<b>KCD_CSV_FILE_TO_INTERN_CONVERT</b>
just check it.
regards
vijay
01-19-2006 10:31 AM
Hi Vijay,
if we use the function module specified by you then wouldn't there be a limitation to upload only 9999 records at once? As kcde_cells-row is of data type N(4).
01-19-2006 10:36 AM
01-19-2006 10:43 AM
Hi Vijay,
then you should switch other solution, if your records more than 9999.
regards
vijay
01-18-2006 8:13 PM
01-19-2006 6:01 AM
Hi Rich & Phani,
Thank you for your inputs.
Phani, I'll let u know once it is done.
01-19-2006 6:06 AM
hi,
you can make use of this FM to upload the csv(comma delimited).
<b>KCD_CSV_FILE_TO_INTERN_CONVERT</b>
give the filename,separator(',' in this case) and itab.
regards
vijay