FORM itabtodataset TABLES intab
USING dname TYPE c.
DATA: tab TYPE x VALUE '09',
enter(2) TYPE x VALUE '0D0A',
count(8) TYPE n.
DATA: BEGIN OF headtab OCCURS 0 ,
length TYPE i ,
decimals TYPE i,
type_kind TYPE c,
name(30) TYPE c,
END OF headtab.
DATA n TYPE i .
DATA descr_ref TYPE REF TO cl_abap_structdescr.
FIELD-SYMBOLS: <comp_wa> TYPE abap_compdescr ,
<f_field> ,
<f_intab> TYPE ANY .
DATA:str TYPE string ,
text1 TYPE c.
DATA:runtime TYPE int4,
repid LIKE sy-repid.
DATA: flen TYPE i.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>,
<dyn_field>.
DATA: dy_table TYPE REF TO data,
dy_line TYPE REF TO data,
xfc TYPE lvc_s_fcat,
ifc TYPE lvc_t_fcat.
descr_ref ?= cl_abap_typedescr=>describe_by_data( intab ).
LOOP AT descr_ref->components ASSIGNING <comp_wa>.
MOVE-CORRESPONDING <comp_wa> TO headtab.
APPEND headtab.
ENDLOOP.
LOOP AT headtab.
flen = flen + headtab-length + headtab-DECIMALS + 1.
ENDLOOP.
CLEAR xfc.
xfc-fieldname = 'LINE' .
xfc-datatype = 'CHAR'. "或者 xfc-inttype = 'C' .
xfc-intlen = flen.
APPEND xfc TO ifc.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = ifc
IMPORTING
ep_table = dy_table.
ASSIGN dy_table->* TO <dyn_table>.
CREATE DATA dy_line LIKE LINE OF <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
ASSIGN COMPONENT 'LINE' OF STRUCTURE <dyn_wa> TO <dyn_field>.
DESCRIBE TABLE headtab LINES n.
LOOP AT intab ASSIGNING <f_intab>.
DO n TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE <f_intab> TO <f_field>.
str = <f_field>.
READ TABLE headtab INDEX sy-index.
IF headtab-type_kind = 'I' OR headtab-type_kind = 'P'
OR headtab-type_kind = 'F'.
SEARCH str FOR '-'.
IF sy-subrc = 0 AND sy-fdpos <> 0.
SPLIT str AT '-' INTO str text1.
CONDENSE str.
CONCATENATE '-' str INTO str.
ELSE.
CONDENSE str.
ENDIF.
ELSE.
* SHIFT str LEFT DELETING LEADING '0' .
ENDIF.
CONCATENATE <dyn_field> tab str INTO <dyn_field>.
ENDDO.
SHIFT <dyn_wa>.
APPEND <dyn_wa> TO <dyn_table>.
CLEAR <dyn_wa>.
count = count + 1 .
ENDLOOP.
OPEN DATASET dname FOR OUTPUT IN BINARY MODE .
IF sy-subrc NE 0.
ENDIF.
LOOP AT <dyn_table> INTO <dyn_wa>.
str = <dyn_wa>.
TRANSFER str TO dname.
TRANSFER enter TO dname.
ENDLOOP.
CLOSE DATASET dname.
ENDFORM.