一:所用到的事物码:
smw0
二:上传步骤
图1
图2
图3
注:“包”为你所在项目的包的名称。
图5
点击“7” 选择Excel模版,就可以将模版上传到sap的服务器上了,另需要记住对象名称,在后面的程序中有需要。
三:下载代码
TABLES:sscrfields.
*定义OLE变量
DATA:g_excel TYPE ole2_object,
g_applica TYPE ole2_object,
g_sheet TYPE ole2_object,
g_cell TYPE ole2_object,
g_workbook TYPE ole2_object.
DATA:p_filepath TYPE string.
CONSTANTS:c_yes TYPE c VALUE 'X',
c_path TYPE char128 VALUE 'C:\TMP\价格信息批量导入.xls'.
*模版下载按钮
SELECTION-SCREEN PUSHBUTTON /1(10) text-002
USER-COMMAND attch.
AT SELECTION-SCREEN.
PERFORM frm_check_input.
*&---------------------------------------------------------------------*
*& Form frm_check_input
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM frm_check_input .
DATA l_ret TYPE abap_bool.
IF sscrfields-ucomm = 'ATTCH'.
* 下载模版
PERFORM frm_download_template.
ELSE.
IF p_filepath IS INITIAL.
MESSAGE s004(zmess) DISPLAY LIKE 'E'.
STOP.
ENDIF.
* 将路径全部转换为大写
TRANSLATE p_filepath TO UPPER CASE.
* 检查文件是否存在
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = p_filepath
RECEIVING
result = l_ret
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
IF l_ret NE c_yes.
MESSAGE e004(zmess) WITH '文件不存在'.
ENDIF.
ENDIF.
ENDFORM. "frm_check_input
*&---------------------------------------------------------------------*
*& Form frm_download_template
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM frm_download_template .
DATA: lv_objdata LIKE wwwdatatab,
lv_obj_name LIKE wwwdatatab-objid,
lv_destination LIKE rlgrap-filename,
lv_objid LIKE sy-repid,
lv_subrc LIKE sy-subrc.
DATA:l_ret TYPE abap_bool,
lv_answer.
DATA:lv_file TYPE string.
MOVE c_path TO lv_file.
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = lv_file
RECEIVING
result = l_ret
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
ENDIF.
IF l_ret EQ c_yes.
* 模版已存在,是否覆盖
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
text_question = '模版已存在,是否覆盖?'
text_button_1 = '是'(001)
text_button_2 = '否'(002)
IMPORTING
answer = lv_answer
EXCEPTIONS
text_not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
ENDIF.
IF lv_answer EQ 'A'. "取消
EXIT.
ELSEIF lv_answer NE '1'. "否
p_filepath = c_path.
ENDIF.
ELSE.
lv_answer = '1'.
ENDIF.
CREATE OBJECT g_excel 'EXCEL.APPLICATION'.
GET PROPERTY OF g_excel 'Workbooks' = g_workbook .
CALL METHOD OF
g_workbook
'Close'.
IF lv_answer EQ '1'.
MOVE 'ZGN_MMB021' TO lv_obj_name.
SELECT relid objid
FROM wwwdata
INTO CORRESPONDING FIELDS OF lv_objdata
UP TO 1 ROWS
WHERE srtf2 = 0 AND relid = 'MI'
AND objid = lv_obj_name.
ENDSELECT.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
key = lv_objdata
destination = c_path
IMPORTING
rc = lv_subrc.
IF lv_subrc = 0.
p_filepath = c_path.
ENDIF.
ENDIF.
CALL METHOD OF
g_workbook
'open'
EXPORTING
#1 = c_path.
CALL METHOD OF
g_excel
'worksheets' = g_sheet
EXPORTING
#1 = 1.
CALL METHOD OF
g_sheet
'activate'.
SET PROPERTY OF g_excel 'visible' = 1.
FREE OBJECT g_sheet.
FREE OBJECT g_applica.
FREE OBJECT g_workbook.
FREE OBJECT g_excel.
ENDFORM. "frm_download_template