10-10-2007 12:34 PM
how to read contens of text file (ASCII), tab separated in a report.
10-10-2007 1:10 PM
hi,
try using this code :
report zbdctest03.
********************************
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.
data: wa_record type t_record.
********************************
call function 'GUI_UPLOAD'
exporting
filename = 'D:\PROGRAM FILES\SAPFILE1.TXT'
filetype = 'ASC'
has_field_separator = 'X'
IMPORTING
FILELENGTH =
HEADER =
tables
data_tab = it_record
exceptions
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
others = 17
.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
*********************************
loop at it_record into wa_record.
write:/ sy-vline,
(10) wa_record-name1,
(20) wa_record-name2,
(30) wa_record-age.
endloop.
*********************************
this might help you.
reward if useful.
____________________________________________________________________________________________________________________________________
10-10-2007 1:13 PM
hi,
There are various FM , and u can use them depending upon the availabilty in ur SAP version,
WS_Upload
Upload
GUI_upload.
You can use the pattern button to get the desired FM, and use it to upload the text file.
<b>Hope this is helpful, Do reward.</b>
10-10-2007 1:28 PM
hi kailash,
To read contents of text files (Ascii) use GUI_UPLOAD function.
GUI_UPLOAD - this FM will Upload the Data of a text File to ITAB.
Please see below the code for it.It is very simple ,plz go through it.
In the code when u call the function GUI_UPLOAD just mention the file type as 'ASCII' as done below.
pls do reward if useful.
Thanks.
types: begin of ttab,
f1(25) type c,
f2(10) type c,
f3(10) type c,
end of ttab.
data: itab type table of ttab 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
FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = 'X'
HEADER_LENGTH = 0
READ_BY_LINE = 'X'
DAT_MODE = ' '
CODEPAGE = ' '
IGNORE_CERR = ABAP_TRUE
REPLACEMENT = '#'
CHECK_BOM = ' '
VIRUS_SCAN_PROFILE =
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.
.
*IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
*ENDIF.
Loop at itab.
write : / itab-f1,itab-f2,itab-f3.
endloop.
10-10-2007 1:33 PM
Hi,
First declare an int table like
data: begin of itab occurs 0,
string(100),
end of itab.
Use the GUI_UPLOAD and put all the text field data into ITAB.Now u can loop at ITAB and split/separate each field/record into different fields at the delimiter by using SPLIT statement and move the fields data into another table and append it.
Reward points for useful Answers.
Regards,
Srilatha.
10-10-2007 2:06 PM
Hi everybody,
The next step after reading the file into an internal table is processing the individual lines of this internal table. There is a generic way of dealing with gthis kind of CSV files, and here is a simple example program. I have included a short part that sets up the internal table for testing purposes, normally you would read the file there...
REPORT zztest.
TYPES:
BEGIN OF target_type,
number1 TYPE i,
text1 TYPE string,
number2 TYPE i,
text2 TYPE string,
END OF target_type.
DATA:
lt_words TYPE TABLE OF string WITH HEADER LINE,
lt_lines TYPE TABLE OF string WITH HEADER LINE,
current_line TYPE string,
target_structure TYPE target_type,
l_index TYPE i.
FIELD-SYMBOLS:
<f> TYPE ANY.
* Set up the test scenario
current_line = '1234;abcd;1234;abcd'.
* Replace the ';' with the TAB character (ASCII 0x09)
REPLACE ALL OCCURRENCES OF ';'
IN current_line
WITH cl_abap_char_utilities=>horizontal_tab.
DO 5 TIMES.
APPEND current_line TO lt_lines.
ENDDO.
* Setup finished, lt_itab now contains 5 identical lines
* each with the contents 1234<tab>abcd<tab>1234<tab>abcd
LOOP AT lt_lines.
REFRESH lt_words.
CLEAR: target_structure, l_index.
SPLIT lt_lines AT cl_abap_char_utilities=>horizontal_tab
INTO TABLE lt_words.
DO.
ADD 1 TO l_index.
ASSIGN COMPONENT l_index
OF STRUCTURE target_structure
TO <f>.
IF sy-subrc NE 0.
EXIT.
ENDIF.
READ TABLE lt_words INDEX l_index.
<f> = lt_words.
ENDDO.
* Now target_structure is populated with the fields from
* the CSV file
WRITE: / 'Number 1',
target_structure-number1,
/ 'Text 1',
target_structure-text1,
/ 'Number 2',
target_structure-number2,
/ 'Text 2',
target_structure-text2.
ENDLOOP.
The code is quite generic. It assumes that the lines in the file are all identical and that there is an ABAP structure with the same fields as in each line, and in the same order.
It also assumes, that the fields have SAP format, i. e. that Dates are represented using YYYYMMDD syntax etc. This way, if the format of the file changes, all you have to change is the ABAP structure (<b>target_type</b> in this example).
Hope this helps,
Regards, Joerg
10-12-2007 9:53 AM