Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

flat file to internal table

Former Member
0 Likes
3,273

Hi all

I have a flat file (abc.txt), how do i import the flat file to the internal table.

Pefer code for the solution.

Thks!

14 REPLIES 14
Read only

Former Member
0 Likes
1,790

Data: begin of itab occurs 0,

lines type string,

End of itab.

Use FM GUI_UPLOAD to upload yout text data into that LINES field of internal table ITAB and after that split it in different fields as per your requirement.

Read only

Former Member
0 Likes
1,790

Hi gary,

Use Fm GUI_Download.

Read only

Former Member
0 Likes
1,790

Hi,

use function module gui_upload

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

FILENAME = S_FILE

FILETYPE = 'ASC'

HAS_FIELD_SEPARATOR = 'X'

TABLES

DATA_TAB = i_vbap.

rgds,

bharat.

Read only

0 Likes
1,790

doubt to query

what is the meaning of

HAS_FIELD_SEPARATOR = 'X'

can give example on this

what is the structure of i_vbap?

How to delcare it?

example to show?

thks

Read only

0 Likes
1,790

Hi,

here is an example of program. Where the fields are seperated by tabs in txt file

<b>Note that :HAS_FIELD_SEPARATOR is not a flag</b>

types:begin of t_input_file,

update(3) type c,

version(10) type c,

data_obj(2) type c,

legacy_id(20) type c,

target_id(20) type c,

target_type(1) type c,

end of t_input_file.

data: i_input_file type standard table of t_input_file.

call function 'GUI_UPLOAD'

exporting

filename = v_filename " type string

has_field_separator = '#' " for tab delimited file

tables

data_tab = i_input_file

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.

" display your message

endif.

Read only

Former Member
0 Likes
1,790

Hi

creat a internal table of structure like ur internal table

and creat that flat file

and di like this

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

FILENAME = LV_FILENAME

FILETYPE = 'ASC'

HAS_FIELD_SEPARATOR = 'X' " Check here

  • HEADER_LENGTH = '1'

  • READ_BY_LINE = 'X'

  • DAT_MODE = ' '

  • CODEPAGE = ' '

  • IGNORE_CERR = ABAP_TRUE

  • REPLACEMENT = '#'

  • CHECK_BOM = ' '

  • IMPORTING

  • FILELENGTH =

  • HEADER =

TABLES

DATA_TAB = IT_COJRNL

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.

reward if usefull

Read only

0 Likes
1,790

how to see the function 'GUI_UPLOAD' ?

HAS_FIELD_SEPARATOR mean what?

abc.txt

field1 X field2 X field3

issit the above eg use in the txt format?

Read only

0 Likes
1,790

Hi,

this is to specify whether there is a seperator between columns/table fields.

It can be ':' , ';' or tab.

Regards

Read only

messier31
Active Contributor
0 Likes
1,790

Hi,

You can use function module GUI_UPLOAD.

<b>Example 1</b>

CALL FUNCTION 'GUI_UPLOAD'

exporting

filetype = 'BIN'

filename = 'C:\DOWNLOAD.BIN'

tables

data_tab = itab.

loads the contents of the file 'C:\DOWNLOAD.BIN' on the frontend PC into the internal backend table itab. The transferred data is not converted.

<b>Example 2</b>

CALL FUNCTION 'GUI_UPLOAD'

exporting

filetype = 'ASC'

filename = 'C:\DOWNLOAD.TXT'

tables

data_tab = itab.

loads the contents of the file 'C:\DOWNLOAD.TXT' on the frontend PC line by line into the internal backend table itab. The transferred data is converted.

Hope this helps.

Enjoy SAP.

Pankaj Singh.

Read only

Former Member
0 Likes
1,790

Hi GAry,

REPORT ZV_TEST_UPLOAD .

PARAMETERS: SP_FILE LIKE RLGRAP-FILENAME.

DATA: BEGIN OF L_T_OUT OCCURS 0,

MATNR LIKE Z48M_MATDATA-MATNR,

ERSDA LIKE MARA-ERSDA,

MAKTX LIKE Z48M_MATDATA-MAKTX,

ZZBCODE LIKE Z48M_MATDATA-ZZBCODE,

WRKST LIKE Z48M_MATDATA-WRKST,

LAENG LIKE MARA-LAENG,

BREIT LIKE MARA-BREIT,

HOEHE LIKE MARA-HOEHE,

ZZELKEN LIKE MARA-ZZELKEN,

MFRNR LIKE MARA-MFRNR,

MFRPN LIKE MARA-MFRPN,

TDETAIL1 LIKE Z48M_MATDATA-MAKTX,

TDETAIL2 LIKE Z48M_MATDATA-MAKTX,

TDETAIL3 LIKE Z48M_MATDATA-MAKTX,

KLART LIKE KSSK-KLART,

CLASS LIKE KLAH-CLASS,

ATINN LIKE AUSP-ATINN,

ATWRT LIKE AUSP-ATWRT,

MATKL LIKE MARA-MATKL,

END OF L_T_OUT.

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

  • CODEPAGE = ' '

FILENAME = SP_FILE

FILETYPE = 'ASC' " Change to DAT

  • HEADLEN = ' '

  • LINE_EXIT = ' '

  • TRUNCLEN = ' '

  • USER_FORM = ' '

  • USER_PROG = ' '

  • DAT_D_FORMAT = ' '

  • IMPORTING

  • FILELENGTH =

TABLES

DATA_TAB = L_T_OUT

EXCEPTIONS

CONVERSION_ERROR = 1

FILE_OPEN_ERROR = 2

FILE_READ_ERROR = 3

INVALID_TYPE = 4

NO_BATCH = 5

UNKNOWN_ERROR = 6

INVALID_TABLE_WIDTH = 7

GUI_REFUSE_FILETRANSFER = 8

CUSTOMER_ERROR = 9

OTHERS = 10

.

IF SY-SUBRC <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

try this editing watever fields u need in ur program.

Hope this helps u.

Regards,

S.Agarwal

Read only

Former Member
0 Likes
1,790

Hi

creat like this

Just create an internal table with your 3 fields
data: begin of ti_data occurs 0,
fld1 type c,
fld2 type c,
fld3 type c,
end of ti_data.

To read the file just call this FM:

ALL FUNCTION 'TB_LIMIT_WS_UPLOAD'
EXPORTING
filename = Root to your file
filetype = 'ASC'
IMPORTING
filelength = length
TABLES
data_tab = ti_data.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

i think this may help you

reward if usefull

Read only

Former Member
0 Likes
1,790

Hiii

u can cal the FM upload.

in that pass the file type and the internal table name.

at the runtime it wil ask for the file name. enter the path of the text file.

regards,

shiva

Read only

0 Likes
1,790

Hi

just create a parameter type given below and provide the f4 help for it.

then create an internal table type the structure of your flat file and by using the Function module you can upload .

just see the below points

*****create an internal table with your flat file structer

TYPES: BEGIN OF ty_cmfile,

quart(1) TYPE c, " Quarter

gjahr(4) TYPE c, " Year

kunnr(10) TYPE c, " Sold to Party

kunwe(10) TYPE c, " Ship to Party

cmcost TYPE zbwcmcost-cmcost, " CM COST

END OF ty_cmfile.

types: ty_t_cmfile TYPE STANDARD TABLE OF ty_cmfile.

data: it_cmfile TYPE ty_t_cmfile.

PARAMETERS : pa_file1 TYPE rlgrap-filename, " Input File

AT SELECTION-SCREEN ON VALUE-REQUEST FOR pa_file1.

  • *** f4 for input file

PERFORM f4_file

USING pa_file1.

start-of-selection

IF NOT pa_file1 IS INITIAL.

PERFORM upload

USING

pa_file1

CHANGING

it_cmfile

z_subrc.

IF z_subrc NE 0.

zn_cancel = 1.

STOP.

ENDIF.

ENDIF. " not pa_file1 is initial.

FORM upload

USING

pafile TYPE rlgrap-filename

CHANGING

pit_cmfile TYPE ty_t_cmfile

p_subrc TYPE sy-subrc.

DATA : lz_file TYPE string, " input file

lit_upload TYPE TABLE OF string,

lwa_cmfile TYPE ty_cmfile,

lwa_upload TYPE string.

CLEAR p_subrc.

MOVE pafile TO lz_file.

  • *** upload the input file into table for customers

  • *** who are having the contract in the quarter

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = lz_file

filetype = kc_asc

has_field_separator = kc_set

TABLES

data_tab = lit_upload

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.

WRITE: 'Error in uploading File'(028).

zn_cancel = 1.

EXIT.

ENDIF.

LOOP AT lit_upload INTO lwa_upload.

SPLIT lwa_upload AT ';' INTO lwa_cmfile-quart lwa_cmfile-gjahr

lwa_cmfile-kunnr lwa_cmfile-kunwe.

APPEND lwa_cmfile TO pit_cmfile.

ENDLOOP.

DELETE pit_cmfile

WHERE quart NE pa_quart

OR gjahr NE pa_gjahr.

IF pit_cmfile[] IS INITIAL.

WRITE / 'No records found for CMCOST for quarter pa_quart.'(030).

p_subrc = 4.

EXIT.

ENDIF.

ENDFORM. " upload

FORM f4_file

USING pa_file TYPE rlgrap-filename.

CLEAR pa_file.

CALL FUNCTION 'F4_FILENAME'

IMPORTING

file_name = pa_file.

ENDFORM. " f4_pafile1

Read only

Former Member
0 Likes
1,790

Hi Gary

you can use a function <b><u>UPLOAD</u></b> , sample code has been attached

CALL FUNCTION 'UPLOAD'

EXPORTING

FILETYPE = 'DAT'

TABLES

data_tab = i_mat.

where : I_MAT is the internal table in which you want to import the data

DAT -


leave it as it is

when you will try to execute it wil ask you the location of the file.