一、在应用服务器中打开文件OPEN DATASET <dsn> [options] 此语句打开文件 <dsn> 。如果不指定任何模式 选项,则文件将按二进 制模式打开。如果系统不能打开文件,则将系统字段 SY-SUBRC 设置为 8,否则 SY-SUBRC 返回 0
二、打开文件读取OPEN DATASET <dsn> FOR INPUT IN TEXT MODE ENCODING DEFAULT .(以TXT文件打开)
三、打开文件写入OPEN DATASET <dsn> FOR OUTPUT IN TEXT MODE ENCODING DEFAULT .(以TXT文件打开写入,这种是打开文件完全重写)
四、打开文件追加 OPEN DATASET <dsn> FOR APPENDING IN TEXT MODE ENCODING DEFAULT .(以TXT文件打开写入,这种是打开文件追加记录)
五、关闭应用服务器上的文件 CLOSE DATASET <dsn>.
六、删除应用服务器上的文件 DELETE DATASET <dsn>.
七、向应用服务器上文件上写入数据TRANSFER <f> to <dsn> [LENGTH <len>].
八、向应用服务器上文件上读取数据READ DATASET <dsn> INTO <f> [LENGTH <len>].
常用事务代码:
1.AL11,服务器上的文件
2.CG3Y 下载文件
3.CG3Z 上传文件
保存内表数据到服务器程序:
REPORT ydownvbak.
TABLES:vbak,vbap.
DATA: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
读取服务器上的文件:
REPORT YDOWNFILE.
data:p_file LIKE rlgrap-filename value '/usr/sap/tmp/SD20111130.txt'.
data:p_line(100).
OPEN DATASET p_file for INPUT in TEXT MODE ENCODING DEFAULT.
if sy-subrc ne 0.
MESSAGE '读取文件出错!' type 'E'.
endif.
do.
READ DATASET p_file INTO p_line.
if sy-subrc ne 0.
exit.
else.
write:/ p_line.
endif.
enddo.
CLOSE DATASET p_file.