*&---------------------------------------------------------------------*
*& Report ZCMH0010
*&
*&---------------------------------------------------------------------*
*& 将内表数据导出到TXT和从TXT读取到内表
*&
*&---------------------------------------------------------------------*
REPORT ZCMH0010.
DATA: BEGIN OF MY_DATA,
MANDT LIKE USR02-MANDT,
BNAME LIKE USR02-BNAME,
ERDAT LIKE USR02-ERDAT,
LTIME LIKE USR02-LTIME,
END OF MY_DATA.
DATA: IT_DATA LIKE MY_DATA OCCURS 0.
data: FILENAME TYPE STRING VALUE 'C:\sap_usr02.txt'.
SELECT USR02~MANDT USR02~BNAME USR02~ERDAT USR02~LTIME
APPENDING TABLE IT_DATA
FROM USR02
UP to 100 rows.
SORT IT_DATA BY BNAME.
*--将内表数据导出到 TXT
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
CONFIRM_OVERWRITE = 'X' "如果文件存在 弹出是否覆盖文件的对话框
WRITE_FIELD_SEPARATOR = 'X' "加入字段分隔符 TAB
FILENAME = FILENAME "文件名 必须为 STRING 类型
TABLES
DATA_TAB = IT_DATA "内表
EXCEPTIONS
FILE_WRITE_ERROR = 1
FILE_NOT_FOUND = 2.
REFRESH IT_DATA. "清空内表
* -- 将TXT 数据读取到内表并显示出来
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
FILENAME = FILENAME "文件名 必须为 STRING 类型
FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = 'X' "可以去掉前面的加入TAB 分隔符,如果不去掉刚会出现#,并且会挤掉后面字段的二位
TABLES
DATA_TAB = IT_DATA. "内表
WRITE: /'MANDT',8'BNAME',25'ERDAT',40'LTIME'.
LOOP AT IT_DATA INTO MY_DATA.
WRITE: / MY_DATA-MANDT,
MY_DATA-BNAME UNDER 'BNAME',
MY_DATA-ERDAT UNDER 'ERDAT',
MY_DATA-LTIME UNDER 'LTIME'.
ENDLOOP.