ABAP顾问
分类:
2007-08-29 15:30:29
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:
DATA:str TYPE string ,
text1 TYPE c.
DATA:runtime TYPE int4,
repid LIKE sy-repid.
DATA: flen TYPE i.
FIELD-SYMBOLS:
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
MOVE-CORRESPONDING
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
CREATE DATA dy_line LIKE LINE OF
ASSIGN dy_line->* TO
ASSIGN COMPONENT 'LINE' OF STRUCTURE
DESCRIBE TABLE headtab LINES n.
LOOP AT intab ASSIGNING
DO n TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE
str =
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
ENDDO.
SHIFT
APPEND
CLEAR
count = count + 1 .
ENDLOOP.
OPEN DATASET dname FOR OUTPUT IN BINARY MODE .
IF sy-subrc NE 0.
ENDIF.
LOOP AT
str =
TRANSFER str TO dname.
TRANSFER enter TO dname.
ENDLOOP.
CLOSE DATASET dname.
ENDFORM.
REPORT z_barry_test.
DATA: BEGIN OF itab OCCURS 0 ,
txt(10) ,
menge LIKE ekpo-menge,
datum LIKE sy-datum,
END OF itab.
itab-txt = 'Lin1'.
itab-menge = '123.45'.
itab-datum = sy-datum.
APPEND itab.
itab-txt = '第二行'.
itab-menge = '0.45'.
itab-datum = sy-datum.
APPEND itab.
itab-txt = '第三行'.
itab-menge = '0.45'.
itab-datum = sy-datum.
APPEND itab.
PERFORM itabtodataset TABLES itab USING 'testbarry.txt'.
*&---------------------------------------------------------------------*
*& Form itabtodataset
*&---------------------------------------------------------------------*
FORM itabtodataset TABLES intab
USING dname TYPE c.
DATA: tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab ,
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:
DATA:str TYPE string ,
cha(100) type c,
text1 TYPE c.
DATA:runtime TYPE int4,
repid LIKE sy-repid.
DATA: flen TYPE i.
FIELD-SYMBOLS:
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
MOVE-CORRESPONDING
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
CREATE DATA dy_line LIKE LINE OF
ASSIGN dy_line->* TO
ASSIGN COMPONENT 'LINE' OF STRUCTURE
DESCRIBE TABLE headtab LINES n.
LOOP AT intab ASSIGNING
DO n TIMES.
ASSIGN COMPONENT sy-index OF STRUCTURE
str =
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.
ELSEIF headtab-type_kind = 'D'.
write
str = cha .
ELSE.
* SHIFT str LEFT DELETING LEADING '0' .
ENDIF.
CONCATENATE
ENDDO.
SHIFT
APPEND
CLEAR
count = count + 1 .
ENDLOOP.
OPEN DATASET dname FOR OUTPUT IN LEGACY TEXT MODE CODE PAGE '8401' WITH WINDOWS LINEFEED.
* OPEN DATASET dname FOR OUTPUT IN TEXT MODE ENCODING UTF-8 WITH WINDOWS LINEFEED .
IF sy-subrc NE 0.
ENDIF.
LOOP AT
str =
TRANSFER str TO dname.
ENDLOOP.
CLOSE DATASET dname.
ENDFORM. "itabtodataset