sap中Excel的模版上传和下载
一:所用到的事物码:
Smw0
二:上传步骤
注:“包”为你所在项目的包的名称。
点击“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
自己的程序:ZPROINFO01
*&---------------------------------------------------------------------*
*& Form temp_excel_get
*&---------------------------------------------------------------------*
* 下载模板
*----------------------------------------------------------------------*
FORM temp_excel_get.
DATA: lo_objdata LIKE wwwdatatab,
lo_mime LIKE w3mime,
lc_filename TYPE string VALUE '程序报表管理模版' ,
lc_fullpath TYPE string VALUE'C:\' ,
lc_path TYPE string VALUE'C:\' ,
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 = 'ZINFO01'. "此处为EXCEL模板名称
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.
* where objid = p_objid.
*检查表wwwdata中是否存在所指定的模板文件
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. "”保存路径
*如果存在,调用DOWNLOAD_WEB_OBJECT 函数下载模板到路径下
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.
* fname = ls_destination.
ENDIF.
ENDFORM. "fm_excel