前言
生成文件(例:txt)保存到服务器,并通过代码保存到本地
一、范例代码
* 先存在服务器 再下载到本地
CLEAR:lt_dir.
SPLIT pm_file AT '\' INTO TABLE lt_dir.
IF lt_dir IS NOT INITIAL.
lv_path = '/usr/sap/tmp/'.
READ TABLE lt_dir INTO ls_dir INDEX lines( lt_dir ).
IF sy-subrc EQ 0.
lv_path = lv_path && ls_dir-dir.
CONDENSE lv_path.
ENDIF.
ENDIF.
pm_file1 = lv_path.
pm_file2 = lv_path.
REPLACE '.txt' WITH 'bkpf.txt' INTO pm_file1.
REPLACE '.txt' WITH 'bseg.txt' INTO pm_file2.
* 使用默认编码格式 避免特殊字符造成异常
OPEN DATASET pm_file1 FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
OPEN DATASET pm_file2 FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
* 查询数据
l_date = so_budat-low.
DO.
IF l_date > so_budat-high.
EXIT.
ENDIF.
CLEAR:lt_where_tab.
CONCATENATE 'BUKRS' 'EQ' 'PM_BUKRS' INTO lv_where_tab SEPARATED BY space.
APPEND lv_where_tab TO lt_where_tab.
lv_where_tab = '''' && '%' && ''''.
CONCATENATE 'AND' 'BSTAT' 'LIKE' lv_where_tab INTO lv_where_tab SEPARATED BY space.
APPEND lv_where_tab TO lt_where_tab.
CONCATENATE 'AND' 'BUDAT' 'EQ' 'L_DATE' INTO lv_where_tab SEPARATED BY space.
APPEND lv_where_tab TO lt_where_tab.
* 新增条件
IF s_blart[] IS NOT INITIAL.
CONCATENATE 'AND' 'BLART' 'IN' 'S_BLART[]' INTO lv_where_tab SEPARATED BY space.
APPEND lv_where_tab TO lt_where_tab.
ENDIF.
IF s_usnam[] IS NOT INITIAL.
CONCATENATE 'AND' 'USNAM' 'IN' 'S_USNAM[]' INTO lv_where_tab SEPARATED BY space.
APPEND lv_where_tab TO lt_where_tab.
ENDIF.
IF s_tcode[] IS NOT INITIAL.
CONCATENATE 'AND' 'TCODE' 'IN' 'S_TCODE[]' INTO lv_where_tab SEPARATED BY space.
APPEND lv_where_tab TO lt_where_tab.
ENDIF.
IF s_xref1[] IS NOT INITIAL. "ADD BY WUSL 20230111
CONCATENATE 'AND' 'XREF1_HD' 'IN' 'S_XREF1[]' INTO lv_where_tab SEPARATED BY space.
APPEND lv_where_tab TO lt_where_tab.
ENDIF.
SELECT bukrs waers bktxt bldat belnr blart cpudt cputm kursf gjahr monat budat tcode usnam bstat stblg xstov stodt stgrd xblnr ausbk
FROM bkpf
INTO CORRESPONDING FIELDS OF TABLE itab_dbkpfout
WHERE (lt_where_tab).
l_date = l_date + 1.
IF itab_dbkpfout[] IS INITIAL.
CONTINUE.
ENDIF.
* 加上字段描述
IF ls_bkpf IS NOT INITIAL.
CONCATENATE ls_bkpf-bukrs ls_bkpf-waers ls_bkpf-bktxt ls_bkpf-bldat ls_bkpf-belnr ls_bkpf-blart
ls_bkpf-cpudt ls_bkpf-cputm ls_bkpf-kursf ls_bkpf-gjahr ls_bkpf-monat ls_bkpf-budat ls_bkpf-tcode
ls_bkpf-usnam ls_bkpf-bstat ls_bkpf-stblg ls_bkpf-xstov ls_bkpf-stodt ls_bkpf-stgrd ls_bkpf-xblnr
ls_bkpf-ausbk
INTO global_field
SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
TRANSFER global_field TO pm_file1.
CLEAR:ls_bkpf.
ENDIF.
LOOP AT itab_dbkpfout.
PERFORM changdata USING itab_dbkpfout.
TRANSFER global_field TO pm_file1.
APPEND itab_dbkpfout TO itab_extbkpf.
ENDLOOP.
SELECT belnr bukrs buzei gjahr hkont koart kostl pswsl saknr sgtxt shkzg wrbtr dmbtr zuonr bschl xnegp augbl mandt buzid pswbt
FROM bseg
INTO CORRESPONDING FIELDS OF TABLE itab_dbsegout
FOR ALL ENTRIES IN itab_dbkpfout
WHERE bukrs = pm_bukrs
AND belnr = itab_dbkpfout-belnr
* AND gjahr = l_date+0(4).
AND gjahr = pm_gjahr.
* 设置字段描述
IF itab_dbsegout[] IS NOT INITIAL AND ls_bseg IS NOT INITIAL.
CONCATENATE ls_bseg-belnr ls_bseg-bukrs ls_bseg-buzei ls_bseg-gjahr ls_bseg-hkont ls_bseg-koart
ls_bseg-kostl ls_bseg-pswsl ls_bseg-saknr ls_bseg-sgtxt ls_bseg-shkzg ls_bseg-wrbtr ls_bseg-dmbtr
ls_bseg-zuonr ls_bseg-bschl ls_bseg-xnegp ls_bseg-augbl ls_bseg-mandt ls_bseg-buzid ls_bseg-pswbt
INTO global_field SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
TRANSFER global_field TO pm_file2.
CLEAR:ls_bseg.
ENDIF.
LOOP AT itab_dbsegout.
PERFORM formatdata USING itab_dbsegout.
PERFORM changdata USING itab_dbsegout.
TRANSFER global_field TO pm_file2.
APPEND itab_dbsegout TO itab_extbseg.
ENDLOOP.
REFRESH: itab_dbkpfout,itab_dbsegout.
ENDDO.
CLOSE DATASET pm_file1.
CLOSE DATASET pm_file2.
IF p_lradio EQ abap_true.
sy-cprog = 'RC1TCG3Y'. "避免下载FM内部报错 下载完后还原为本来程序名
* 改为先存在服务器 随后下载到本地的方式
lv_path = pm_file.
REPLACE '.txt' WITH 'bkpf.txt' INTO lv_path.
lv_file = pm_file1.
CALL FUNCTION 'C13Z_FILE_DOWNLOAD_BINARY'
EXPORTING
i_file_front_end = lv_path
i_file_appl = lv_file
i_file_overwrite = abap_true
IMPORTING
e_flg_open_error = l_error
e_os_message = l_omessage
EXCEPTIONS
fe_file_open_error = 1
fe_file_exists = 2
fe_file_write_error = 3
ap_no_authority = 4
ap_file_open_error = 5
ap_file_empty = 6
ap_path_error = 7
OTHERS = 8.
IF sy-subrc <> 0.
* Implement suitable error handling here
CASE sy-subrc.
WHEN 6.
MESSAGE w000(zfi_0100) WITH '未查找到相关数据!请检查'.
WHEN OTHERS.
MESSAGE e000(zfi_0100) WITH '下载错误'.
ENDCASE.
ENDIF.
lv_path = pm_file.
REPLACE '.txt' WITH 'bseg.txt' INTO lv_path.
lv_file = pm_file2.
CALL FUNCTION 'C13Z_FILE_DOWNLOAD_BINARY'
EXPORTING
i_file_front_end = lv_path
i_file_appl = lv_file
i_file_overwrite = abap_true
IMPORTING
e_flg_open_error = l_error
e_os_message = l_omessage
EXCEPTIONS
fe_file_open_error = 1
fe_file_exists = 2
fe_file_write_error = 3
ap_no_authority = 4
ap_file_open_error = 5
ap_file_empty = 6
ap_path_error = 7
OTHERS = 8.
IF sy-subrc <> 0.
* Implement suitable error handling hereCASE sy-subrc.
CASE sy-subrc.
WHEN 6.
MESSAGE w000(zfi_0100) WITH '未查找到相关数据!请检查'.
WHEN OTHERS.
MESSAGE e000(zfi_0100) WITH '下载错误'.
ENDCASE.
ENDIF.
sy-cprog = 'ZTEST_FILE'.
ENDIF.
总结
需要有服务器文件读取权限 也可COPY一个Z的C13Z下载FUNCTION 把权限校验部分去掉