两种函数 :
1.DOWNLOAD_WEB_OBJECT 文件名不能超过128位
* DATA: lo_objdata LIKE wwwdatatab,
* lo_mime LIKE w3mime,
* lc_filename TYPE string VALUE '生产版本导入模板',
* lc_fullpath TYPE string,
* lc_path TYPE string,
* ls_destination LIKE rlgrap-filename,
* ls_objnam TYPE string,
* li_rc LIKE sy-subrc,
* ls_errtxt TYPE string.
* DATA:p_objid TYPE wwwdatatab-objid,
* p_dest LIKE sapb-sappfad.
*
* p_objid = 'ZPP007'.
*
* CALL METHOD cl_gui_frontend_services=>file_save_dialog
* EXPORTING
* default_extension = 'XLS'
* default_file_name = lc_filename
* CHANGING
* filename = lc_filename
* path = lc_path
* fullpath = lc_fullpath
* EXCEPTIONS
* cntl_error = 1
* error_no_gui = 2
* not_supported_by_gui = 3
* OTHERS = 4.
* IF lc_fullpath = ''.
* MESSAGE '不能打开excel' TYPE 'E'.
* ENDIF.
* IF sy-subrc = 0.
* p_dest = lc_fullpath.
** concatenate p_objid '.XLS' into ls_objnam.
* CONDENSE ls_objnam NO-GAPS.
* SELECT SINGLE relid objid FROM wwwdata INTO CORRESPONDING FIELDS OF lo_objdata
* WHERE srtf2 = 0 AND relid = 'MI' AND objid = p_objid.
* IF sy-subrc NE 0 OR lo_objdata-objid EQ space.
* CONCATENATE '模板文件' ls_objnam '不存在' INTO ls_errtxt.
* MESSAGE ls_errtxt TYPE 'I'.
* ENDIF.
* ls_destination = p_dest.
* CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
* EXPORTING
* key = lo_objdata
* destination = ls_destination
* IMPORTING
* rc = li_rc.
* IF li_rc NE 0.
* CONCATENATE '模板文件:' ls_objnam '下载失败' INTO ls_errtxt.
* MESSAGE ls_errtxt TYPE 'E'.
* ENDIF.
* ENDIF.
2.GUI_DOWNLOAD
DATA: l_filename TYPE string,
l_fullpath TYPE string,
l_path TYPE string,
l_key TYPE wwwdatatab,
l_mime TYPE TABLE OF w3mime,
l_default_file_name TYPE string.
l_key-relid = 'MI'.
l_key-objid = 'ZPP007'.
l_default_file_name = '生产版本导入模板'.
CALL FUNCTION 'WWWDATA_IMPORT'
EXPORTING
key = l_key
TABLES
mime = l_mime
EXCEPTIONS
wrong_object_type = 1
import_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = '文件下载'
default_extension = '.xls'
default_file_name = l_default_file_name
file_filter = cl_gui_frontend_services=>filetype_excel
CHANGING
filename = l_filename
path = l_path
fullpath = l_fullpath
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
IF l_fullpath IS INITIAL .
RETURN .
ENDIF.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = l_fullpath
filetype = 'BIN'
TABLES
data_tab = l_mime
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
OTHERS = 22.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
DOWNLOAD_WEB_OBJECT 完整示列
TABLES:rlgrap,sscrfields.
SELECTION-SCREEN FUNCTION KEY 1.
PARAMETERS: p_file LIKE rlgrap-filename . "选择文件路径
INITIALIZATION.
sscrfields-functxt_01 = '下载导入模板'.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. "获取文件路径
PERFORM frm_file_help CHANGING p_file.
AT SELECTION-SCREEN.
CASE sscrfields-ucomm.
WHEN 'FC01'.
PERFORM frm_download..
ENDCASE.
FORM frm_file_help CHANGING pv_file TYPE rlgrap-filename.
CALL FUNCTION 'F4_FILENAME'
IMPORTING
file_name = pv_file.
ENDFORM.
FORM frm_download .
DATA: objdata TYPE wwwdatatab,
lv_path TYPE rlgrap-filename,
lv_subrc TYPE sy-subrc.
DATA:lv_xlsid TYPE w3objid.
lv_xlsid = 'ZFI_ZCMXCF'.
" 下载
CLEAR: lv_path, objdata.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
def_path = 'E:\'
mask = ',Excel file,*.xls;*.xlsx;' " 限制为Excel文件 --
mode = 'S'
title = '下载Excel模版……'
IMPORTING
filename = lv_path " 返回的文件名 --
EXCEPTIONS
inv_winsys = 1
no_batch = 2
selection_cancel = 3
selection_error = 4
OTHERS = 5.
CLEAR lv_subrc.
lv_subrc = sy-subrc.
IF lv_subrc = 0.
" 检查文件是否存在
SELECT SINGLE relid objid INTO CORRESPONDING FIELDS OF objdata
FROM wwwdata WHERE srtf2 EQ 0 AND relid EQ 'MI' AND objid EQ lv_xlsid.
IF ( sy-subrc NE 0 ) OR ( objdata-objid IS INITIAL ).
MESSAGE '模板不存在' TYPE 'E'.
EXIT.
ENDIF.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = objdata
destination = lv_path
IMPORTING
rc = lv_subrc.
IF lv_subrc NE 0.
MESSAGE '模板下载失败' TYPE 'E'.
ELSE.
MESSAGE '模板下载成功' TYPE 'S'.
ENDIF.
ENDIF.
ENDFORM.
FORM frm_get_excel .
DATA :ls_data TYPE alsmex_tabline,
lt_data TYPE TABLE OF alsmex_tabline INITIAL SIZE 2000.
DATA: lv_col TYPE i,
ls_xls TYPE REF TO data,
ls_dat TYPE REF TO data.
FIELD-SYMBOLS: <fs_val> TYPE any,
<fs_dat> TYPE any,
<fs_xls> TYPE any.
* CLEAR:gt_data,gs_data,gt_upload.
" 数据从EXCEL导入内表 --
REFRESH lt_data.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = p_file
i_begin_col = '1'
i_begin_row = '2'
i_end_col = '2'
i_end_row = '9999'
TABLES
intern = lt_data
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc NE 0.
MESSAGE '上载文件失败!' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ELSE.
* CREATE DATA ls_xls LIKE LINE OF gt_upload.
ASSIGN ls_xls->* TO <fs_xls>.
LOOP AT lt_data INTO ls_data.
MOVE ls_data-col TO lv_col.
ASSIGN COMPONENT lv_col OF STRUCTURE <fs_xls> TO <fs_val>.
IF ls_data-col = '1'.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = ls_data-value
IMPORTING
output = ls_data-value
EXCEPTIONS
length_error = 1.
ENDIF.
MOVE ls_data-value TO <fs_val>.
AT END OF row.
* APPEND <fs_xls> TO gt_upload.
CLEAR <fs_xls>.
ENDAT.
CLEAR :ls_data,lv_col.
ENDLOOP.
ENDIF.
* CLEAR:gt_data.
* MOVE-CORRESPONDING gt_upload TO gt_data.
ENDFORM.
GUI_DOWNLOAD 示例
*&---------------------------------------------------------------------*
*& Report YDOWN
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ydown.
DATA: filename TYPE string,
u_action TYPE i.
DATA:BEGIN OF fname OCCURS 0 ,
colum(100) ,
END OF fname .
SELECT
*
INTO TABLE @DATA(lt_vbap)
FROM vbap
UP TO 200 ROWS.
* 下载路径和文件名选择
CALL FUNCTION 'GUI_FILE_SAVE_DIALOG'
EXPORTING
window_title = TEXT-g01
IMPORTING
filename = filename
user_action = u_action.
IF u_action EQ '9' .
* CANCEL
EXIT .
ENDIF .
"第一行数据的描述
fname[] = VALUE #(
( colum = '客户端' )
( colum = '销售订单' )
( colum = '行号' )
).
* 下载
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = filename
write_field_separator = 'X'
no_auth_check = 'X'
TABLES
data_tab = lt_vbap "lt_total
fieldnames = fname.