参数:
IV_OBJID TYPE WWWDATA-OBJID SAP WWW 网关对象名
IV_WINDOW_TITLE TYPE STRING
IV_FILENAME TYPE STRING
IV_FILE_FILTER TYPE STRING
value( IV_ISOPEN ) TYPE BOOLEAN OPTIONAL 布尔变量(X= 真,-= 假,空格 = 未知)
方法代码:
METHOD open_smw0_template.
DATA: lv_fielname TYPE string,
lv_path TYPE string,
lv_fullpath TYPE string,
lv_dest TYPE rlgrap-filename.
DATA: lv_objid TYPE wwwdata-objid.
DATA: lo_objdata TYPE wwwdatatab.
DATA: lv_rc TYPE sy-subrc.
DATA:
lo_excel TYPE ole2_object,
lo_workbook TYPE ole2_object,
lo_worksheet TYPE ole2_object,
lo_columns TYPE ole2_object.
IF iv_objid IS INITIAL.
RETURN.
ENDIF.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = iv_window_title
default_extension = 'xls'
default_file_name = iv_filename
file_filter = iv_file_filter
CHANGING
filename = lv_fielname
path = lv_path
fullpath = lv_fullpath
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
IF sy-subrc <> 0 OR lv_fullpath IS INITIAL.
RETURN.
ENDIF.
lv_dest = lv_fullpath.
SELECT SINGLE relid objid FROM wwwdata
INTO CORRESPONDING FIELDS OF lo_objdata
WHERE srtf2 = 0
AND relid = 'MI'
AND objid = iv_objid.
IF lv_rc NE 0.
MESSAGE '模板文件下载失败' TYPE 'S' DISPLAY LIKE 'E'.
RETURN.
ENDIF.
CLEAR lv_rc.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = lo_objdata
destination = lv_dest
IMPORTING
rc = lv_rc.
IF lv_rc NE 0.
MESSAGE '模板文件下载失败' TYPE 'S' DISPLAY LIKE 'E'.
RETURN.
ENDIF.
IF iv_isopen IS INITIAL.
RETURN.
ENDIF.
CREATE OBJECT lo_excel 'EXCEL.APPLICATION'.
IF sy-subrc <> 0.
RETURN.
ENDIF.
CALL METHOD OF
lo_excel
'WORKBOOKS' = lo_workbook.
CALL METHOD OF
lo_workbook
'OPEN'
EXPORTING
#1 = lv_fullpath.
IF sy-subrc <> 0.
RETURN.
ENDIF.
SET PROPERTY OF lo_excel 'VISIBLE' = 2.
GET PROPERTY OF lo_excel 'ACTIVESHEET' = lo_worksheet.
GET PROPERTY OF lo_excel 'ACTIVEWORKBOOK' = lo_workbook.
SET PROPERTY OF lo_excel 'DISPLAYALERTS' = 0.
CALL METHOD OF lo_excel 'COLUMNS' = lo_columns
EXPORTING
#1 = 'A:D'.
CALL METHOD OF lo_columns 'AutoFit'.
CALL METHOD OF
lo_workbook
'SAVES'
EXPORTING
#1 = lv_fullpath
#2 = 1.
ENDMETHOD.
调用 :
CONSTANTS:
cns_objid TYPE wwwdata-objid VALUE 'ZMM1000F011',
cns_wintitle TYPE string VALUE '项目计划维护模板',
cns_fname TYPE string VALUE '项目计划维护模板',
cns_ffilter TYPE string VALUE '(电子表格EXCEL)',
cns_mcode TYPE bapi2017_gm_code VALUE '04'.
zcl_comm_tools=>open_smw0_template(
iv_objid = cns_objid
iv_window_title = cns_wintitle
iv_filename = cns_fname
iv_file_filter = cns_ffilter
iv_isopen = abap_true ).