Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
giusfg32
Explorer
6,174

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://blogs.sap.com/2021/06/09/easy-way-to-download-internal-table-data-to-an-excel-file-in-the-pr...

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.
12 Comments
shais
Participant
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).
abo
Active Contributor
Thanks for the code, not a bad idea despite the limitations already mentioned by shais

However, the suggested link for Excel export.... well, there's room for improvement, as noted in the comment section of that blog post 😉
giusfg32
Explorer
0 Kudos
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 😊
giusfg32
Explorer
0 Kudos
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) 😀
0 Kudos
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

 

 

 
hugo_degroot2
Participant

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:

  method download_t005_to_excel.

data:
country_tab type standard table of t005.

data:
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' ) .
endmethod.

 

Cheers,

---Hugo

giusfg32
Explorer
0 Kudos
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.



with



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:



CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
EXPORTING
i_field_seperator = ';'
i_line_header = lv_header_line
TABLES
i_tab_sap_data = lt_table
CHANGING
i_tab_converted_data = itab1
EXCEPTIONS
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.
ENDIF.



Hope this can help you 🙂 .


Giuseppe
giusfg32
Explorer
Hi Hugo,

thank you very much for this comment.

I will try as soon as possible 😉 .


Thank you,

Giuseppe
0 Kudos

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



hardyp180
Active Contributor
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
christian_brhl
Explorer
0 Kudos
"https://github.com/abap2xlsx/abap2xlsx" has been around for quite awhile and has more than passed the acid test.
Labels in this area