SAP_ABAP_寫資料到SAP伺服器檔案並讀取
阿新 • • 發佈:2019-01-09
BEGIN OF wa_itab ,
vbeln LIKE vbak-vbeln,
vkorg LIKE vbak-vkorg,
erdat LIKE vbak-erdat,
kunnr LIKE vbak-kunnr,
posnr LIKE vbap-posnr,
matnr LIKE vbap-matnr,
kwmeng LIKE vbap-kwmeng,
END OF wa_itab.
DATA:itab LIKE wa_itab OCCURS 0 WITH HEADER LINE.
DATA:g_file LIKE rlgrap-filename.
DATA:s_file LIKE rlgrap-filename.
DATA:menge(20).
DATA:line(1000) TYPE c.
SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE t1.
SELECT-OPTIONS:s_erdat FOR vbak-erdat,
s_vbeln FOR vbak-vbeln,
s_kunnr FOR vbak-kunnr,
s_vkorg FOR vbak-vkorg.
PARAMETERS:p_client AS CHECKBOX,
p_dnfile LIKE rlgrap-filename DEFAULT 'D:\',
p_server AS CHECKBOX,
p_sefile LIKE rlgrap-filename DEFAULT '/usr/sap/tmp/'.
SELECTION-SCREEN END OF BLOCK bl1 .
INITIALIZATION.
t1 = '選擇條件'.
START-OF-SELECTION.
PERFORM sub_read_data.
END-OF-SELECTION.
PERFORM sub_write_data.
*&---------------------------------------------------------------------*
*& Form SUB_READ_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM sub_read_data .
SELECT vbak~vbeln vkorg vbak~erdat kunnr posnr matnr kwmeng
INTO TABLE itab
FROM vbak
JOIN vbap ON vbap~vbeln = vbak~vbeln
WHERE vbak~vbeln IN s_vbeln
AND vbak~erdat IN s_erdat
AND vkorg IN s_vkorg
AND kunnr IN s_kunnr.
ENDFORM. " SUB_READ_DATA
*&---------------------------------------------------------------------*
*& Form SUB_WRITE_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM sub_write_data .
IF p_client = ' X'.
CONCATENATE p_dnfile 'SD' sy-datum sy-uzeit '.TXT' INTO g_file. "文字格式
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
filename = g_file
filetype = 'DAT'
TABLES
data_tab = itab. "被下載的內表
ENDIF.
IF p_server = 'X'.
CONCATENATE p_sefile 'SD' sy-datum '.txt' INTO s_file. "伺服器儲存的目錄和檔名
OPEN DATASET s_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT . "開啟檔案
if sy-subrc = 0.
LOOP AT itab INTO wa_itab.
menge = wa_itab-kwmeng.
CONCATENATE wa_itab-vkorg
wa_itab-kunnr
wa_itab-vbeln
wa_itab-erdat
wa_itab-posnr
wa_itab-matnr
menge
INTO line SEPARATED BY ''.
TRANSFER line TO s_file .
IF sy-subrc NE 0 .
MESSAGE 'ERROR!!' type 'E'.
ENDIF.
CLEAR line .
ENDLOOP.
CLOSE DATASET s_file.
else.
MESSAGE 'OPEN ERROR!!' type 'E'.
endif.
ENDIF.
ENDFORM. " SUB_WRITE_DATA