Export SAP Data to Fixed Length Text File
Using WS_DOWNLOAD ABAP function module to export data from SAP to text file is a common method of transferring data from SAP to external systems. Writing data to text file from internal table in ABAP program is easy. In this ABAP tutorial, I'll share source codes of an ABAP report which reads data from SAP tables and write contents of table data in a text file. ABAP function module WS_DOWNLOAD can be used to create text file in a specific file folder and write contents of the internal table into text file.
Below ABAP program query SAP VBRP Sales document items table for SAP sales invoices for a given range of invoice numbers. This is just to insert sample SAP data into an ABAP internal table. Since I'll not place all table columns into text file first I create a TYPE object and created a structure and an table instance for thi type.
The OSQL SELECT statement insert sample items by reading from VBRP Sales document item table to internal table gt_vbrp. Within an ABAP LOOP statement, I concatenate related columns into a single field text structure which is big enough to store all desired table fields.
The structure with single text field which has the string concatenation of all related table fields is type of ty_vbrp formed of 1000 characters long C field. ABAP developers can store the CONCATENATE function output into gs_text structure for single row. And all concatenated rows are stored in an internal table named gt_text
Although this tutorial is concentrated on fixed length output field values on exported text file, I used a seperator character "|" between table fields. Of course the ABAP programmer can remove the SEPARATED BY clause from the CONCATENATE command since all fields have known length values. Since length of each column is already known, it will be easy for the programmer who will parse the exported SAP data in text file.
The main trick in CONCATENATE statement that provides fixed length in output text is RESPECTING BLANKS statement. This prevents auto trim of spaces in character fields by Concatenate command. You can test by removing RESPECTING BLANKS clause from the Concatenate statement and compare both execution results to see the affect in action.
I also added a row end character ";" to indicate the end of each row. Of course this is again optional just like field seperator between column values. You can ommit this code block if you don't need a row seperator.
Following ABAP codes are related with WS_DOWNLOAD ABAP function call. First of all we need a network share or file folder that we can write text file as the output of WS_DOWNLOAD ABAP function module. This file folder is provided as the value of "filename" as an input parameter to ws_download function module.
And the last part of the sample ABAP code in this ABAP tutorial is the ABAP function module WS_DOWNLOAD which write internal table contents into text file. This is extremely and easy method to export SAP data to text file for ABAP developers.
In below screenshot, I showed the output of the text file exported from SAP system VBRP table data and written in text file on a given folder or network share.