Skip to Content
Technical Articles
Author's profile photo Giuseppe Gentile


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)

  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.
    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.

      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.

          tabname    = lv_table_name
          langu      = sy-langu
          lfieldname = lv_field_name
          all_types  = 'X'
          dfies_wa   = ls_dfies
          not_found  = 1
          OTHERS     = 3.
      CASE sy-subrc.
        WHEN 0.
          CLEAR ls_name.
          IF ls_dfies-fieldtext NE ''.
            ls_name-label = ls_dfies-fieldtext.
            ls_name-label = ''.
          APPEND ls_name TO lt_name.
        WHEN 1.
          RAISE not_found.
        WHEN OTHERS.
          RAISE internal_error.



  CLEAR ls_name.

  LOOP AT lt_name INTO ls_name.
    IF sy-tabix EQ 1.
      lv_header = ls_name-label.
      CONCATENATE lv_header ';' ls_name-label INTO lv_header.

  APPEND lv_header TO lt_file.



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:

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

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Shai Sinai
      Shai Sinai

      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).

      Author's profile photo Giuseppe Gentile
      Giuseppe Gentile
      Blog Post Author
      Hi Shai,
      your observation is right. As I specified in the post, the code I wrote is a draft I used to produce csv files like this:
      Thank you so much for the helpful comment 😊
      Author's profile photo Andrea Borgia
      Andrea Borgia

      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 😉

      Author's profile photo Giuseppe Gentile
      Giuseppe Gentile
      Blog Post Author
      Hello Andrea, thanks for the comment.
      You're right about the links, my intent was just to point out the information contained in the two posts (including comments) 😀
      Author's profile photo Anne d'Hooge
      Anne d'Hooge

      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).








      Author's profile photo Giuseppe Gentile
      Giuseppe Gentile
      Blog Post Author
      Hi Anne,
      without seeing your code I don't know how to help you.
      I can tell you that I noticed an error: I wrote the code in Italian and then translated it. I forgot to replace
      DATA lv_testata (15000) TYPE c.
      DATA lv_header (15000) TYPE c.
      As for using the standard FM, I wrote the following code before the IF lv_header_line EQ 'X' statement:
          i_field_seperator    = ';'
          i_line_header        = lv_header_line
          i_tab_sap_data       = lt_table
          i_tab_converted_data = itab1
          conversion_failed    = 1
          OTHERS               = 2.
      IF sy-subrc <> 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1
        sy-msgv2 sy-msgv3 sy-msgv4.
      Hope this can help you 🙂 .
      Author's profile photo Anne d'Hooge
      Anne d'Hooge


      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 ?


      Author's profile photo Anne d'Hooge
      Anne d'Hooge



      lv_header is ok

      Author's profile photo Hugo de Groot
      Hugo de Groot

      Hi Giuseppe,

      You should look into abap2xlsx !

      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:

        method download_t005_to_excel.
            country_tab type standard table of t005.
            excel     type ref to zcl_excel,
            worksheet type ref to zcl_excel_worksheet.
          "Populate country tab
          select * from t005 into table country_tab.
          "Create Excel workbook
          create object excel.
          "Populate worksheet
          worksheet = excel->get_active_worksheet( ) .
          worksheet->set_title( ip_title = 'Table T005') .
          worksheet->bind_table( ip_table = country_tab ) .
          "Display workbook
          zcl_excel_output=>output( cl_excel = excel
                                    i_output_type = 'DISPLAY_ONLINE'
                                    i_path = 'C:\temp\T005.xlsx'  ) .




      Author's profile photo Giuseppe Gentile
      Giuseppe Gentile
      Blog Post Author
      Hi Hugo,
      thank you very much for this comment.
      I will try as soon as possible 😉 .
      Thank you,
      Author's profile photo Paul Hardy
      Paul Hardy

      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


      Author's profile photo Christian Brühl
      Christian Brühl

      "" has been around for quite awhile and has more than passed the acid test.