08-01-2007 12:08 PM
I have one file in the presentation server and data will be in TAB delemeter format...
How to split the data when the delemeter is TAB.
Can any one give me the solution asap..
Thanks and regards,
M.R.Reddy
08-01-2007 12:15 PM
Hi Rayapu,
Create an intenal table with exactly the same structure like data in the file and use FM: UPLOAD <b>or</b>
FM: GUI_UPLOAD <b>or</b>
CALL METHOD cl_gui_frontend_services=>gui_upload
e.g:
CALL FUNCTION 'UPLOAD'
EXPORTING
filename = 'C:\test.txt '
filetype = 'DAT'
TABLES
data_tab = t_tab.
Regards,
Hendy
08-01-2007 12:15 PM
Hi Rayapu,
Create an intenal table with exactly the same structure like data in the file and use FM: UPLOAD <b>or</b>
FM: GUI_UPLOAD <b>or</b>
CALL METHOD cl_gui_frontend_services=>gui_upload
e.g:
CALL FUNCTION 'UPLOAD'
EXPORTING
filename = 'C:\test.txt '
filetype = 'DAT'
TABLES
data_tab = t_tab.
Regards,
Hendy
08-01-2007 12:17 PM
Hi,
Here is the example Program
*&---------------------------------------------------------------------*
*& Report ZUPLOADTAB *
*& *
*&---------------------------------------------------------------------*
*& Example of Uploading tab delimited file *
*& *
*&---------------------------------------------------------------------*
REPORT zuploadtab .
PARAMETERS: p_infile LIKE rlgrap-filename
OBLIGATORY DEFAULT '/usr/sap/'..
*DATA: ld_file LIKE rlgrap-filename.
DATA: gd_file type string.
*Internal tabe to store upload data
TYPES: BEGIN OF t_record,
name1 LIKE pa0002-vorna,
name2 LIKE pa0002-name2,
age TYPE i,
END OF t_record.
DATA: it_record TYPE STANDARD TABLE OF t_record INITIAL SIZE 0,
wa_record TYPE t_record.
*Internal table to upload data into
DATA: BEGIN OF it_datatab OCCURS 0,
row(500) TYPE c,
END OF it_datatab.
*Text version of data table
TYPES: BEGIN OF t_uploadtxt,
name1(10) TYPE c,
name2(15) TYPE c,
age(5) TYPE c,
END OF t_uploadtxt.
DATA: wa_uploadtxt TYPE t_uploadtxt.
*String value to data in initially.
DATA: wa_string(255) TYPE c.
CONSTANTS: con_tab TYPE x VALUE '09'.
*If you have Unicode check active in program attributes then you will
*need to declare constants as follows:
*class cl_abap_char_utilities definition load.
*constants:
* con_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB.
************************************************************************
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_INFILE.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_infile.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_filename = p_infile
mask = ',*.txt.'
mode = 'O'
title = 'Upload File'(078)
IMPORTING
filename = p_infile
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
************************************************************************
*START-OF-SELECTION
START-OF-SELECTION.
gd_file = p_infile.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = gd_file
has_field_separator = 'X' "file is TAB delimited
TABLES
data_tab = it_record
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 NE 0.
write: 'Error ', sy-subrc, 'returned from GUI_UPLOAD FM'.
skip.
endif.
* Alternative method, where by you split fields at each TAB after you
* have returned the data. No point unless you dont have access to
* GUI_UPLOAD but just included for information
*
* CALL FUNCTION 'GUI_UPLOAD'
* EXPORTING
* filename = gd_file
* filetype = 'ASC'
* TABLES
* data_tab = it_datatab "ITBL_IN_RECORD[]
* EXCEPTIONS
* file_open_error = 1
* OTHERS = 2.
* IF sy-subrc NE 0.
* ELSE.
* LOOP AT it_datatab.
* CLEAR: wa_string, wa_uploadtxt.
* wa_string = it_datatab.
* SPLIT wa_string AT con_tab INTO wa_uploadtxt-name1
* wa_uploadtxt-name2
* wa_uploadtxt-age.
* MOVE-CORRESPONDING wa_uploadtxt TO wa_record.
* APPEND wa_record TO it_record.
* ENDLOOP.
* ENDIF.
************************************************************************
*END-OF-SELECTION
END-OF-SELECTION.
*!! Text data is now contained within the internal table IT_RECORD
* Display report data for illustration purposes
LOOP AT it_record INTO wa_record.
WRITE:/ sy-vline,
(10) wa_record-name1, sy-vline,
(10) wa_record-name2, sy-vline,
(10) wa_record-age, sy-vline.
ENDLOOP.
Regards
Sudheer
08-01-2007 12:17 PM
report ztest_0001.
parameters: d1 type localfile default '/usr/sap/TST/SYS/Test.txt'.
data: begin of itab occurs 0,
field1(20) type c,
field2(20) type c,
field3(20) type c,
end of itab.
data: str type string.
constants: con_tab type x value '09'.
if you have a newer version, then you can use this instead.
*constants:
con_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB.
start-of-selection.
itab-field1 = 'ABC'.
itab-field2 = 'DEF'.
itab-field3 = 'GHI'.
append itab.
itab-field1 = '123'.
itab-field2 = '456'.
itab-field3 = '789'.
append itab.
open dataset d1 for output in text mode.
loop at itab.
concatenate itab-field1 itab-field2 itab-field2 into str
separated by con_tab.
transfer str to d1.
endloop.
close dataset d1.
Regards,
Pavan
08-01-2007 12:20 PM
Declare internal table
Data: begin of itab occurs 0,
field1(10) type C,
field2(5) type c,
field3 type vbak-vbeln,
...
end of itab.
u can use GUI_UPLOAD to get the data in internal tbale.
give the filename in GUI_UPLOAD and file type as ASC
and store in ur internal table
08-01-2007 12:25 PM
Use FM : GUI_UPLOAD and give field separator parameter as 'X'.
Regards
Prax