Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos
6,981

Hi,

I was working on a requirement where I need to create a CSV file using APD in application server and insert a custom header.

The header insertion can be achieved using a ABAP program, also I found documents in SDN to achieve the same.

The issue with available logic in SDN is we need to create a separate temporary file in application server and then only we can insert the custom header.

Using below code we can insert the custom header without creating a temporary file in application server.

*&---------------------------------------------------------------------*

*& Report ZBW_INSERT_CUSTOM_HEADER

*&

*&---------------------------------------------------------------------*

*&

*& To insert a custom header into a CSV file generated by Analysis Process Designer in application server.

*&

*&

*&---------------------------------------------------------------------*

REPORT ZBW_INSERT_CUSTOM_HEADER.

*If file is LOGICAL

DATA: logical_file(60) type c VALUE 'ZBW_LOGICAL_FILE',    // Give logical file name.

file(60) TYPE C ,

result TYPE string,

temp TABLE TYPE string.

*If file is physical saved in local system

DATA: file(60) type c VALUE 'c:\user\desktop\file.csv',     // Give path of local desktop file.

result TYPE string,

temp TABLE TYPE string.

* Convert logical path and file name into physical path and file name

* Get physical file names

CALL FUNCTION 'FILE_GET_NAME'

   EXPORTING

     logical_filename = logical_file

   IMPORTING

     file_name        = file.

OPEN DATASET file FOR INPUT IN TEXT MODE

ENCODING DEFAULT

WITH SMART LINEFEED.

* Read logical file data into temporary table.

CLEAR temp.

DO.

   READ DATASET file INTO result.

   IF sy-subrc = 0.

     APPEND result TO temp.

   ELSE.

     EXIT.

   ENDIF.

ENDDO.

CLOSE DATASET file.

DELETE DATASET file.

OPEN DATASET file FOR APPENDING IN TEXT MODE

ENCODING DEFAULT

WITH SMART LINEFEED.

* Insert custom header to logical file

SET DATASET file POSITION 0.

TRANSFER `cost centre;cost centre description;posting period;posting year;keyfigure1;keyfigure2;` TO file.


CLOSE DATASET file.

OPEN DATASET file FOR APPENDING IN TEXT MODE

ENCODING DEFAULT

WITH SMART LINEFEED.

* Transfer Temporary file data to Logical file.

DO.

   READ TABLE temp INTO result INDEX 1.

   IF sy-subrc = 0.

     TRANSFER result TO file.

     DELETE temp INDEX 1.

   ELSE.

     EXIT.

   ENDIF.

ENDDO.

CLOSE DATASET file.

Please note to include this program in process chain after APD execution.

Start Variant --> APD Execution --> Program Variant

Thanks.

2 Comments
Labels in this area