SAP ABAP將內表資料匯出到TXT和從TXT讀取到內表
阿新 • • 發佈:2019-01-03
*&---------------------------------------------------------------------*
*& 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.
*& 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~
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 =
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.