OLE—下载模板

上传excel模板 Tcode smw0

OLE—下载模板_第1张图片
Paste_Image.png

进去之后选择一个对应的包(本地的包为 $TMP)
新建,从路径导入

OLE—下载模板_第2张图片
Paste_Image.png

上传路径定义PARAMETERS: P_PATH LIKE RLGRAP-FILENAME

上传路径.png

  • 例子,下载一个模板

给选择屏幕加按钮
DATA gs_funx TYPE smp_dyntxt. "选择屏幕按钮

gs_funx-icon_text = '下载模板'.
sscrfields-functxt_01 = gs_funx.```


![效果.png](http://upload-images.jianshu.io/upload_images/3459413-05856746eb7a927a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

*--------------------------------------------------ole 参数-----------------------------------
-------------------*
```DATA: gv_excel  TYPE OBJ_RECORD,  " EXCEL OBJECT        "OLE2
      gv_mapl   TYPE OBJ_RECORD,  " LIST OF WORKBOOKS   "OLE2
      gv_map    TYPE OBJ_RECORD,  " WORKBOOK            "OLE2
      gv_zl     TYPE OBJ_RECORD,  " CELL                "OLE2
      gv_f      TYPE OBJ_RECORD,  " FONT                "OLE2
      gv_column TYPE OBJ_RECORD.                        "OLE2```

```DATA: gv_sheet       TYPE OBJ_RECORD,                   "OLE2
      gv_activesheet TYPE OBJ_RECORD,                   "OLE2
      gv_newsheet    TYPE OBJ_RECORD,                   "OLE2
      gv_appl        TYPE OBJ_RECORD,                   "OLE2
      gv_work        TYPE OBJ_RECORD,                   "OLE2
      gv_cell        TYPE OBJ_RECORD.                   "OLE2```
```DATA: gv_mod TYPE wwwdata-objid. "下载文件名SMW0```

```INITIALIZATION. "初始化选择屏幕
gv_mod = 'ZJOB_14062'."给gv_mod的值为上传的模板```

在屏幕操作需加这一步,如果在alv里面则在user_commend里面写
```AT SELECTION-SCREEN.     "选择屏幕按钮响应
  CASE sscrfields-ucomm.
    WHEN 'FC01'.
      PERFORM frm_download.
  ENDCASE.```

```form frm_download .
*&---变量定义
  DATA: lv_text       TYPE string,                    "message
        lv_filename   TYPE string,                    "download file name
        lv_path       TYPE string,                    "download file path
        ls_functxt    TYPE smp_dyntxt,                "menu name
        lv_filepath   TYPE rlgrap-filename VALUE 'c', "file patch
        lv_title      TYPE string ,                   "title
        lv_title_name TYPE string,                    "title name
        lv_mod(20)    TYPE c,                         "model
        lv_fullpath   TYPE string,                    "full path
        ls_object     TYPE wwwdatatab,                "object name
        lv_rc         TYPE sy-subrc,                 "return code
        l_init_fname TYPE string.```

```* 初始名称(输出的文件名称)
  CONCATENATE 'ole练习' sy-datum '.xls' INTO l_init_fname.```

```*&---SMWO 模板名称
  lv_mod = gv_mod. "下载文件名
*&---调用OS 操作系统对话框
  lv_title      = text-002.
  lv_title_name = text-003.
  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      window_title      = lv_title      " 保存'
      default_file_name = l_init_fname " 客户合作伙伴导入模板
    CHANGING
      filename          = lv_filename
      path              = lv_path
      fullpath          = lv_fullpath.
*&---操作系统文件路径
  CONCATENATE lv_path
  lv_filename
  '.xlsx'
  INTO lv_filepath.
*&---OS长度控制/*&---下载模板
  IF strlen( lv_filepath ) <> 4.
*&---查询模板.
    SELECT SINGLE
      relid objid
    FROM wwwdata
    INTO CORRESPONDING FIELDS OF ls_object
    WHERE srtf2 = '0'
      AND objid = lv_mod.                         "smw0里对象名称
    IF sy-subrc <> 0 OR ls_object-objid = space .
      CONCATENATE text-911"'模板文件:'
      lv_mod
      text-912"'不存在,请用smw0进行加载.'
      INTO lv_text.
      MESSAGE lv_text TYPE 'E'.
      STOP.
    ENDIF.
*&---下载模板
    CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
      EXPORTING
        key         = ls_object
        destination = lv_filepath
      IMPORTING
        rc          = lv_rc.
    IF lv_rc <> 0.
      CONCATENATE text-911"''模板文件:'
      lv_mod
      text-913"''下载失败,请与开发人员联系.'
      INTO lv_text.
      MESSAGE lv_text TYPE 'E'.
      STOP.
    ENDIF.
*&---'已下载'
    MESSAGE text-106 TYPE 'S'.
  ELSE.
*&---'已取消下载'
    MESSAGE text-107 TYPE 'S'.
  ENDIF.
ENDFORM.    ```
都为标准程序,可以直接引用。

![点击下载模板执行界面.png](http://upload-images.jianshu.io/upload_images/3459413-a704ffc0c31caf1d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)







你可能感兴趣的:(OLE—下载模板)