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
chinaunix网友2009-12-05 21:38:19
TTCall是由新博网络有限公司集即时通讯、网络电话、短信、影视、网络游戏集一体的大型娱乐平台。她操作简单,语音加密,实时计费,话音质量可与传统电话,即可实现低至0.2元拨打全国的固话、小灵通与手机。 网内语音服务 使用TTCall可以与您的好友网内免费通话。全球无漫游拨打好友小号! 语音稳定清晰 超一流语音通话,让您享受清晰的通话质量,卓越音质,畅享沟通! 录音功能 通话过程中,您可以点击“录音”按钮进行通话录音。 变声功能: 绝对独家“魔音”功能,“男生变女声、女生变男声”。 网络电视: 上百家娱乐、新闻电视台扑面而至。 精彩大片、网络游戏游戏 通过TTCall您可以观看大片,精神不容错过,同时可以玩热门游戏。 TTCall网址:http://ttcall.tti5.com 软件下载:http://down.tti5.com
sytrydor2008-04-22 13:04:00
谢谢老白,用你给的方法已能实现: REPORT z_barry_test1234 . TYPES truxs_t_text_data(4096) TYPE c OCCURS 0. DATA: BEGIN OF itab OCCURS 0 , f1(10) TYPE c, f2(20) TYPE c, f3 TYPE i , END OF itab. DATA: csvtab TYPE truxs_t_text_data . START-OF-SELECTION. itab-f1 = 'aa'. itab-f2 = 'bb'. itab-f3 = '15'. APPEND itab. itab-f1 = 'cc'. itab-f2 = 'dd'. itab-f3 = 23. APPEND itab. PERFORM data TABLES itab csvtab. CALL F