Tip

Read TAB delimited text file in ABAP

If you have data in an Excel file that needs to be used for creating a batch input session, you normally save the Excel file as a TAB delimited text file. Then this file needs to be read by an ABAP program to create a batchinput session. In this tip I would like to show how to do this. The code was developed and tested in SAP release 4.6B.


report ysstabc .
**********************************************************************
* If you have data in an Excel file that needs to be used for        *
* creating a batch input session, then save the Excel file as a text *
* file(Tab delimited). This program will read the tab delimited text *
* file and transfer the required fields in an internal table which   *
* can be used for creating a batch input session.                    *
**********************************************************************
constants: begin of tabc,
            tabchar(1) type x value '09',
           end of tabc.
*
parameters:
   pcfile(128) obligatory
               default 'c:temptestfile.txt'.
*
data: begin of i_tab occurs 0,
       rec(200)         type c,
      end of i_tab.
*
data: ftprecs type i,
      flength type p,
      msg_text(50) type c.
************************************************************************
* Define input fields for batchinput in structure inrec                *
************************************************************************
data: begin of inrec occurs 0,
       matnr(18)                type c, "Material

    Requires Free Membership to View

number mtart(40) type c, "Material type matkl(4) type c, "Material group descr(40) type c, "Material description um(3) type c, "Unit of measure end of inrec. ************************************************************************ * The first record in the TAB delimited text input file must contain * * the column names. * * Now in the structure 'fidx' define the field names for fields * * defined in structure 'inrec'. These names must correspond to the col-* * umn names in the input file. * ************************************************************************ data: begin of fidx, "Field index matnr-i type i, matnr-n(20) type c value 'Material', mtart-i type i, mtart-n(20) type c value 'Type', matkl-i type i, matkl-n(20) type c value 'Group', descr-i type i, descr-n(20) type c value 'Descr', um-i type i, um-n(20) type c value 'UM', end of fidx. * data: w_idx type sy-index, w_err type i. * data: begin of i_ftab occurs 0, field(60) type c, end of i_ftab. * field-symbols: <f1>, <f2>. *----------------------------------------------------------------------* start-of-selection. *----------------------------------------------------------------------* perform upload-pcfile. * perform do-conversion. * if w_err ne 0. write: / 'Errors encountered in conversion!!!'. stop. endif. * loop at inrec. * * Put here batch input creation code * * endloop. * *---------------------------------------------------------------------* * FORM do-conversion * *---------------------------------------------------------------------* * ........ * *---------------------------------------------------------------------* form do-conversion. ************************************************************************ * This program prepares an internal table 'inrec' reading the 'i_tab' * * inputfile. In the 'inrec' table we need to select only the necessary * * fields for batchinput. * ************************************************************************ loop at i_tab. split i_tab-rec at tabc into table i_ftab. if sy-tabix eq 1. perform build-field-index. if w_err is initial. continue. else. exit. endif. endif. clear inrec. do. assign component sy-index of structure inrec to <f1>. if sy-subrc ne 0. exit. "No more fields in inrec endif. w_idx = ( sy-index * 2 ) - 1 . assign component w_idx of structure fidx to <f2>. if <f2> gt 0. read table i_ftab index <f2>. if sy-subrc = 0. move i_ftab to <f1>. endif. endif. enddo. append inrec. endloop. endform. *---------------------------------------------------------------------* * FORM build-field-index * *---------------------------------------------------------------------* * ........ * *---------------------------------------------------------------------* form build-field-index. field-symbols: <i1>, <i2>. data: l_idx type i, l_idx2 type i. clear l_idx. do. add 2 to l_idx. assign component l_idx of structure fidx to <i1>. if sy-subrc ne 0. exit. endif. l_idx2 = l_idx - 1 . assign component l_idx2 of structure fidx to <i2>. read table i_ftab with key field = <i1>. if sy-subrc eq 0. move sy-tabix to <i2>. else. skip. write: / 'Error: Column',<i1>,'not defined in input file'. move 1 to w_err. endif. enddo. endform. *---------------------------------------------------------------------* * FORM upload-pcfile * *---------------------------------------------------------------------* * ........ * *---------------------------------------------------------------------* form upload-pcfile. call function 'WS_UPLOAD' exporting filename = pcfile importing filelength = flength tables data_tab = i_tab exceptions file_conversion_error = 1 file_open_error = 2 file_read_error = 3 invalid_table_width = 4 invalid_type = 5 no_batch = 6 unknown_error = 7 others = 8. case sy-subrc. when 1. write: 'file-open error'. when 2. write: 'file-read error'. when 3. write: 'file invalid type'. when 4. write: 'invalid table width'. when 5. write: 'invalid type'. when 6. write: 'No batch'. when 7. write: 'Unknown error'. when 8. write: 'Other error'. endcase. if flength le 0. write: / 'File (PC) uploaded is empty'. stop. endif. endform.

This was first published in August 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.