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.
BEGIN OF ty_vbrp,
vbeln LIKE vbrk-vbeln,
posnr LIKE vbrp-posnr,
matnr LIKE vbrp-matnr,
ean11 LIKE vbrp-ean11,
fkimg LIKE vbrp-fkimg,
vrkme LIKE vbrp-vrkme,
erdat LIKE vbrk-erdat,
netwr LIKE vbrp-netwr,
END OF ty_vbrp,
BEGIN OF ty_text,
text(1000) TYPE c,
END OF ty_text.
DATA c_separator TYPE c1 VALUE '|'.
DATA gs_vbrp TYPE ty_vbrp.
DATA gt_vbrp TYPE TABLE OF ty_vbrp.
DATA gs_text TYPE ty_text.
DATA gt_text TYPE TABLE OF ty_text.
lv_fkimg(17) TYPE c,
lv_netwr(21) TYPE c.
SELECT * INTO CORRESPONDING FIELDS OF TABLE gt_vbrp FROM vbrp
WHERE vbeln BETWEEN '0030000130' AND '0030000140'.
LOOP AT gt_vbrp INTO gs_vbrp.
WRITE gs_vbrp-fkimg TO lv_fkimg DECIMALS 3.
WRITE gs_vbrp-netwr TO lv_netwr DECIMALS 2.
SEPARATED BY c_separator.
CONCATENATE gs_text ';' INTO gs_text.
APPEND gs_text TO gt_text.
DATA filename LIKE rlgrap-filename .
filename = '\\kodyaz\c$\abap-codes\invoice-items.txt'.
CALL FUNCTION 'WS_DOWNLOAD'
filename = filename
filetype = 'ASC'
data_tab = gt_text.
IF sy-subrc <> 0.
* Implement suitable error handling here
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.