2007 May 23 10:19 AM
Hello friends,
How to upload a CSV file into internal table and provide data to BAPI?
Could you give one example.
2007 May 23 11:00 AM
Use the code as given below. In the end u will have the data in the internal table inputtab and u can pass this internal table into the required BAPI as desired.
TYPES : BEGIN OF tp_data,
line(4096),
END OF tp_data.
FORM gui_upload.
*--Local data declaration
DATA: l_filename_ip TYPE string,
tl_data TYPE STANDARD TABLE OF tp_data WITH HEADER LINE.
l_filename_ip = p_input.
*--Upload Data
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = l_filename_ip
filetype = 'ASC'
has_field_separator = ''
TABLES
data_tab = tl_data
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.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
IF sy-subrc = 1.
MESSAGE e000 WITH text-e04.
ENDIF.
ELSE.
LOOP AT tl_data.
PERFORM split_and_save USING tl_data-line.
ENDLOOP.
ENDIF.
ENDFORM. " gui_upload
&----
*& Form split_and_save
&----
Splitting the contents from the input file and saving them into
an internal table
----
-->P_TL_DATA_LINE text
----
FORM split_and_save USING fp_data.
*--Local data declaration
DATA : tl_data TYPE STANDARD TABLE OF tp_data WITH HEADER LINE,
l_type TYPE c,
c_comma(1) TYPE c VALUE ','.
*--Split at the Comma
SPLIT fp_data AT c_comma INTO TABLE tl_data.
CLEAR inputtab.
*--Move it to the target fields
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE inputtab TO <fs_field>.
IF NOT sy-subrc IS INITIAL.
EXIT.
ENDIF.
*--Extract source data
CLEAR tl_data.
READ TABLE tl_data INDEX sy-index.
*--Populate the target
<fs_field> = tl_data-line.
ENDDO.
*--Append this record
APPEND inputtab.
ENDFORM. " split_and_save
2007 May 23 10:25 AM
Hi
Use This FM TEXT_CONVERT_XLS_TO_SAP.
Create internal table according to the specific structure..Then Pass This Internal Table to BAPI...
Reward All Helpfull ANswers.....
2007 May 23 10:27 AM
can u uplad csv to internal table using fm Gui_upload fm and when u use 'TEXT_CONVERT_XLS_TO_SAP' use below stm
TYPE-POOLS: truxs.
DATA: it_raw TYPE truxs_t_text_data.
regards,
sathish
2007 May 23 10:28 AM
Hi
You can do this way.
Have a internal table with a field called data_in and specify the length as the total length of each line in your input file say 300 chars.
1. Use FM GUI_UPLOAD to get the records from file into your internal table.
2. Create an internal table to hold the data from the file with appriopriate lengths.
3. Now, loop on the internal table, and use SPLIT AT command to split data in DATA_IN field into the fields of workarea of type second internal table.
Append this workarea to second internal table.
for eg:
types: begin of flat_itab,
rec(500) type c,
end of flat_itab.
Data: begin of new_itab occurs 0,
field1(10) type c,
field2(20) type c,
field3(10) type c,
field4(10) type c,
field5(10) type c,
field6(10) type c,
field7(10) type c,
field8(100) type c,
end of new_itab.
data: data_tab type table of flat_itab.
data: fname type string.
fname = 'C:\sun\rs3text.csv'.
call method cl_gui_frontend_services=>gui_upload
exporting
filename = fname
changing
data_tab = data_tab
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.
write sy-subrc.
loop at data_tab.
split data_itab at ',' into new_itab-field1
new_itab-field2
new_itab-field3
new_itab-field4
new_itab-field5
new_itab-field6
new_itab-field7
new_itab-field8.
append new_itab.
endloop.
Regards,
Raj
2007 May 23 10:54 AM
http://www.sapdevelopment.co.uk/file/file_upexcel.htm
There are also a couple of alternatives which use function modules 'KCD_EXCEL_OLE_TO_INT_CONVERT'
and 'ALSM_EXCEL_TO_INTERNAL_TABLE' but the method in this link is by far the simplest method to be used.
Award points if found helpful
2007 May 23 11:00 AM
Use the code as given below. In the end u will have the data in the internal table inputtab and u can pass this internal table into the required BAPI as desired.
TYPES : BEGIN OF tp_data,
line(4096),
END OF tp_data.
FORM gui_upload.
*--Local data declaration
DATA: l_filename_ip TYPE string,
tl_data TYPE STANDARD TABLE OF tp_data WITH HEADER LINE.
l_filename_ip = p_input.
*--Upload Data
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = l_filename_ip
filetype = 'ASC'
has_field_separator = ''
TABLES
data_tab = tl_data
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.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
IF sy-subrc = 1.
MESSAGE e000 WITH text-e04.
ENDIF.
ELSE.
LOOP AT tl_data.
PERFORM split_and_save USING tl_data-line.
ENDLOOP.
ENDIF.
ENDFORM. " gui_upload
&----
*& Form split_and_save
&----
Splitting the contents from the input file and saving them into
an internal table
----
-->P_TL_DATA_LINE text
----
FORM split_and_save USING fp_data.
*--Local data declaration
DATA : tl_data TYPE STANDARD TABLE OF tp_data WITH HEADER LINE,
l_type TYPE c,
c_comma(1) TYPE c VALUE ','.
*--Split at the Comma
SPLIT fp_data AT c_comma INTO TABLE tl_data.
CLEAR inputtab.
*--Move it to the target fields
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE inputtab TO <fs_field>.
IF NOT sy-subrc IS INITIAL.
EXIT.
ENDIF.
*--Extract source data
CLEAR tl_data.
READ TABLE tl_data INDEX sy-index.
*--Populate the target
<fs_field> = tl_data-line.
ENDDO.
*--Append this record
APPEND inputtab.
ENDFORM. " split_and_save
2011 Jun 16 2:15 PM