Application Development 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: 

How to read a flat file into a report?

Former Member
0 Kudos
134

The file format is like below:

Company Code : BUKRS(header)

Posting Period :3(header)

Then The items data is started as a table format.

Line items in table format.

If anybody knows the answer please share with me.

5 REPLIES 5

Former Member
0 Kudos
92

Hi ,

If the file is on presentation server then use Function modules GUI_UPLOAD or if it is on application server then use Datasets.

Now declare a internal table of single field to hold a single line. Loop through the internal table and using offset segregate the data into fields.

Hope this helps.

anversha_s
Active Contributor
0 Kudos
92

hi adiseshi,

chk this sample.

&----


*& 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.

*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.

*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.

************************************************************************

*START-OF-SELECTION

START-OF-SELECTION.

ld_file = p_infile.

OPEN DATASET ld_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc NE 0.

ELSE.

DO.

CLEAR: wa_string, wa_uploadtxt.

READ DATASET ld_file INTO wa_string.

IF sy-subrc NE 0.

EXIT.

ELSE.

SPLIT wa_string AT con_tab INTO wa_uploadtxt-name1

wa_uploadtxt-name2

wa_uploadtxt-age.

MOVE-CORRESPONDING wa_uploadtxt TO wa_upload.

APPEND wa_upload TO it_record.

ENDIF.

ENDDO.

CLOSE DATASET ld_file.

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.

rgds

anver

if hlped mark points

Former Member
0 Kudos
92

Use the GUI_UPLOAD Program to upload the file from Presentation server to SAP.

Sample code:

&----


*& Report Z_UPLOAD_MUNCPCODE *

*& *

&----


*& *

*& *

&----


REPORT z_upload_muncpcode.

PARAMETERS : p_fname LIKE rlgrap-filename.

TYPES: BEGIN OF ty_munc,

land1 TYPE tzone-land1,

zone1 TYPE tzone-zone1,

vtext TYPE tzont-vtext,

END OF ty_munc.

DATA: i_munc TYPE STANDARD TABLE OF ty_munc,

i_tzone TYPE STANDARD TABLE OF tzone,

i_tzont TYPE STANDARD TABLE OF tzont,

wa_munc TYPE ty_munc,

wa_tzone TYPE tzone,

wa_tzont TYPE tzont.

CONSTANTS: c_path TYPE char20 VALUE 'C:\',

c_mask TYPE char9 VALUE ',*.*,*.*.',

c_mode TYPE char1 VALUE 'O',

c_filetype TYPE char10 VALUE 'ASC',

c_x TYPE char01 VALUE 'X'.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

*-- Browse Presentation Server

PERFORM f4_presentation_file.

START-OF-SELECTION..

*-- Read presentation server file

PERFORM f1003_pre_file.

LOOP AT i_munc INTO wa_munc.

wa_tzone-mandt = wa_tzont-mandt = sy-mandt.

wa_tzone-land1 = wa_tzont-land1 = wa_munc-land1.

wa_tzone-zone1 = wa_tzont-zone1 = wa_munc-zone1.

wa_tzont-spras = sy-langu.

wa_tzont-vtext = wa_munc-vtext.

APPEND wa_tzont TO i_tzont.

APPEND wa_tzone TO i_tzone.

CLEAR: wa_munc, wa_tzont, wa_tzone.

ENDLOOP.

END-OF-SELECTION.

Modify Table TZONT

PERFORM enqueue_table USING text-001.

MODIFY tzont FROM TABLE i_tzont.

PERFORM dequeue_table USING text-001.

Modify Table TZONE

PERFORM enqueue_table USING text-002.

MODIFY tzone FROM TABLE i_tzone.

PERFORM dequeue_table USING text-002.

WRITE: 'Tables TZONE & TZONT are updated'.

*&----


*& Form f4_presentation_file

*&----


*& F4 Help for presentation server

*&----


FORM f4_presentation_file .

CALL FUNCTION 'WS_FILENAME_GET'

EXPORTING

def_path = c_path

mask = c_mask

mode = c_mode

title = text-001

IMPORTING

filename = p_fname

EXCEPTIONS

inv_winsys = 1

no_batch = 2

selection_cancel = 3

selection_error = 4

OTHERS = 5.

IF sy-subrc <> 0.

  • MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

  • WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ENDFORM. " f4_presentation_file

*&----


*& Form f1003_pre_file

*&----


*& Upload File

*&----


FORM f1003_pre_file .

DATA: lcl_filename TYPE string.

lcl_filename = p_fname.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = lcl_filename

filetype = c_filetype

has_field_separator = c_x

TABLES

data_tab = i_munc

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.

EXIT.

ENDIF.

ENDFORM. " f1003_pre_file

&----


*& Form enqueue_table

&----


*& Enqueue Table

&----


FORM enqueue_table USING p_tabname.

CALL FUNCTION 'ENQUEUE_E_TABLE'

EXPORTING

tabname = p_tabname

EXCEPTIONS

foreign_lock = 1

system_failure = 2

OTHERS = 3.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

ENDFORM. " enqueue_table

&----


*& Form dequeue_table

&----


*& Dequeue Table

&----


FORM dequeue_table USING p_tabname.

CALL FUNCTION 'DEQUEUE_E_TABLE'

EXPORTING

tabname = p_tabname.

ENDFORM. " dequeue_table

Prakash.

Former Member
0 Kudos
92

call function 'gui_upload'

exporting

filename = 'C:\test.txt'

tables

data_tab = itab.

loop at itab.

if itab-line CS 'Company Code' OR

itab-line CS 'Posting Period'.

**Header data

ELSE.

**line data.

endif.

endloop.

Former Member
0 Kudos
92

Hi

Try this for uploading files from the presentation Server.

  • Retrieve data file from presentation server(Upload from PC)

DATA: i_file like rlgrap-filename value 'c:\test_file.txt'. " Replace this by your own file name and complete path in the presentation server

DATA: begin of it_datatab occurs 0,

bukrs like bkpf-bukrs,

budat like bkpf-budat,

end of it_datatab.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = i_file

filetype = 'ASC'

TABLES

data_tab = it_datatab

EXCEPTIONS

file_open_error = 1

OTHERS = 2.