作者:冷瞳流行精品服装店 | 来源:互联网 | 2023-09-03 21:20
前面有说过如何如何将EXCEL模板上传到SAP里,那么使用代码的话如何实现下载EXCEL模板*&-----------------------------------
前面有说过如何如何将EXCEL 模板上传到SAP里,
那么使用代码的话如何实现下载EXCEL模板
*&---------------------------------------------------------------------*
*& Report ZTEST09
*& by cmh
*&---------------------------------------------------------------------*
*&
*& 下载 EXCEL 模板
*&---------------------------------------------------------------------*
REPORT ZTEST09.
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:\TEMP\资产负债表.xls'. " 下载后存放路径
*模版下载按钮
SELECTION-SCREEN BEGIN OF block block1 with frame title text-000.
parameters : g_filen like wwwdatatab-objid default 'ZFI045_1000'. " 要下载的模板名称
SELECTION-SCREEN PUSHBUTTON /1(10) text-002
USER-COMMAND attch.
SELECTION-SCREEN end of block block1.
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.
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 g_filen 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