FM SAP_CONVERT_TO_CSV_FORMAT: Get Table Header

Hello everyone, I want to share with you some information (hopefully useful) about exporting a table to a .csv file in ABAP.

While debugging, I noticed that many people don’t use the standard SAP_CONVERT_TO_CSV_FORMAT Function Module to export tables to a .csv file.

Investigating the reason I saw that in the versions of SAP I use there is a comment according to which the standard FM does not manage the possibility of inserting a header line in the .csv file.

The FM SAP_CONVERT_TO_CSV_FORMAT has, in fact, an input parameter I_LINE_HEADER which, if set, would allow you to insert the header line in the file. However, examining the code I found this comment within the FM SAP_CONVERT_TO_TEX_FORMAT (called within the FM SAP_CONVERT_TO_CSV_FORMAT):

To overcome this obstacle, I have written a few lines of code (a draft), which I want to share with you, which allows you to obtain the header line and add it to the .csv file using the table inserted as input in the FM SAP_CONVERT_TO_CSV_FORMAT:

(I specify that this code is a part of a larger code section)

DATA: l_help_id LIKE tline-tdline, l_struc_index LIKE sy-index. DATA ls_dfies TYPE dfies. DATA: itab1 TYPE truxs_t_text_data. DATA: BEGIN OF ls_name, label TYPE string, END OF ls_name, lt_name LIKE STANDARD TABLE OF ls_name. DATA: lv_table_name TYPE ddobjname, lv_field_name TYPE dfies-lfieldname. DATA: BEGIN OF ls_file, lv_header(15000) TYPE c, END OF ls_file, lt_file LIKE STANDARD TABLE OF ls_file. FIELD-SYMBOLS: <f_source>. DATA: lt_table TYPE TABLE OF "table to export", ls_table LIKE LINE OF lt_table. DATA lv_header_line TYPE c. DATA lv_testata(15000) TYPE c. REFRESH: lt_name[], lt_table[].
CLEAR: l_struc_index, lv_header_line. lt_table[] = "table to export". IF lv_header_line EQ 'X'. READ TABLE lt_table INTO ls_table INDEX 1. DO. l_struc_index = l_struc_index + 1. UNASSIGN <f_source>. ASSIGN COMPONENT l_struc_index OF STRUCTURE ls_table TO <f_source>. IF sy-subrc <> 0. EXIT. ELSE. ASSIGN COMPONENT l_struc_index OF STRUCTURE ls_table TO <f_source>. DESCRIBE FIELD <f_source> HELP-ID l_help_id. CONDENSE l_help_id. CLEAR: lv_table_name, lv_field_name. SPLIT l_help_id AT '-' INTO lv_table_name lv_field_name. CALL FUNCTION 'DDIF_FIELDINFO_GET' EXPORTING tabname = lv_table_name langu = sy-langu lfieldname = lv_field_name all_types = 'X' IMPORTING dfies_wa = ls_dfies EXCEPTIONS not_found = 1 OTHERS = 3. CASE sy-subrc. WHEN 0. CLEAR ls_name. IF ls_dfies-fieldtext NE ''. ls_name-label = ls_dfies-fieldtext. ELSE. ls_name-label = ''. ENDIF. APPEND ls_name TO lt_name. WHEN 1. RAISE not_found. WHEN OTHERS. RAISE internal_error. ENDCASE. ENDIF. ENDDO. CLEAR ls_name. LOOP AT lt_name INTO ls_name. IF sy-tabix EQ 1. lv_header = ls_name-label. ELSE. CONCATENATE lv_header ';' ls_name-label INTO lv_header. ENDIF. ENDLOOP. APPEND lv_header TO lt_file. ENDIF. 

At this point we can hang the converted file with the FM SAP_CONVERT_TO_CSV_FORMAT to lt_file.

In summary, in this post I wanted to share a code that I wrote to get the header to add to a csv file produced by an FM implemented by SAP (since this function module does not manage this functionality)

I am also writing two links related to posts in which we talk about exporting an internal table to an Excel file:

https://blogs.sap.com/2021/06/09/easy-way-to-download-internal-table-data-to-an-excel-file-in-the-presentation-server/

https://answers.sap.com/questions/4315311/download-internal-table-to-excel-with-header.html

I hope this information will be useful to you.
See you in the next post.