Technical Articles
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_header(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://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.
Thanks for sharing your solution.
Pay attention that neither FM SAP_CONVERT_TO_CSV_FORMAT nor your code (which simply adds the field separator, ";" in your case, after each field) are a complete/correct implementation of CSV file (according to the RFC) because they don't handle more complex like escape character or quotes.
e.g. value1;part2, value2 should be generated as "value1;part2";value2 (and not value1;part2;value2).
Thanks for the code, not a bad idea despite the limitations already mentioned by Shai Sinai
However, the suggested link for Excel export.... well, there's room for improvement, as noted in the comment section of that blog post 😉
Hi Giuseppe,
This looks great. However, I coudn't add the converted file itab1 to lt_file
"ITAB1" cannot be converted to the row type of "LT_FILE". The reverse is also not possible.
Did I miss something ? (sorry, it's been a while since I wrote in abap).
thanks
Anne
Hi,
I noticed the lv_testata. I found my mistake however it's not working correctly, I took debug screenshots (attached) after 'append lv_header to lt_file'. Can you take a look ?
lv_header is ok
Hi Giuseppe,
You should look into abap2xlsx !
https://blogs.sap.com/2010/07/12/abap2xlsx-generate-your-professional-excel-spreadsheet-from-abap/
With just a few lines you can bind your internal table to an Excel worksheet and output it, either in Excel in-place or to a file on your PC or or on the app server. You'll get automatically the column headings from the data dictionary. (You can also define your own headings in a so-called "field catalog".) Existence of an installed Excel program on the PC or app server is not required.
Here is an example:
Â
Cheers,
---Hugo
I would just like the second the motion that you look into ABAP2XLSX
In essence for the last ten years there has been one blog very month (two this month) on the SCN about downloading ABAP internal tables into a file that can be read by Excel.
As you will see in the links I have been challenged to try and fight this by writing a series of blogs myself about ABAP2XLSX, as clearly even after ten years no-one has heard of it.
In the end I think I am actually going to have to do this. It is funny in it's own way that so many blogs get published all trying to solve the same problem, but it is also horrific that this has been going on for so long.
So, when my next blog is done (it is about CL_SALV_TABLE and editability BTW another recurring theme) and published then it will be time to see if I cannot be like King Canute and turn back the "Download Internal table from ABAP" tide of blogs.
Cheersy Cheers
Paul
"https://github.com/abap2xlsx/abap2xlsx" has been around for quite awhile and has more than passed the acid test.