Hello everyone, I want to share with you some information (hopefully useful) about exporting a table to a .csv file in ABAP.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
3 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 | |
1 |