SAP ABAP excel文件模板上传下载

一:事物码smw0

二:上传步骤

程序源码:

sql 复制代码
l_filename = 'XX.xls'.
     l_muban = 'z123'.
*&---下载模板
      PERFORM frm_get_fullpath CHANGING gv_fullpath gv_path gv_name.
*&---路径为空则退出
      IF gv_fullpath IS INITIAL.
        MESSAGE '用户取消操作' TYPE 'S'.
        RETURN.
      ENDIF.
      PERFORM frm_down USING gv_fullpath.
*--------------------------------------------------------------------*
*       获取下载到本地的路径
*--------------------------------------------------------------------*
FORM frm_get_fullpath   CHANGING pv_fullpath TYPE string
                                 pv_path     TYPE string
                                 pv_name     TYPE string.
DATA: lv_init_path  TYPE string,
        lv_init_fname TYPE string,
        lv_path       TYPE string,
        lv_filename   TYPE string,
        lv_fullpath   TYPE string.
*&---初始名称(输出的文件名称)
*  concatenate 'Material_Doc_' SY-DATUM '.xslx' into L_INIT_FNAME.
  lv_init_fname = l_filename.
* 获取桌面路径
  CALL METHOD cl_gui_frontend_services=>get_desktop_directory
    CHANGING
      desktop_directory    = lv_init_path
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    EXIT.
  ENDIF.
*&---用户选择名称、路径
  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
*     window_title         = '指定保存文件名'
*     default_extension    = 'DOC'
      default_file_name    = lv_init_fname
*     FILE_FILTER          = CL_GUI_FRONTEND_SERVICES=>FILETYPE_EXCEL
*     FILE_FILTER          = CL_GUI_FRONTEND_SERVICES=>FILETYPE_WORD
      initial_directory    = lv_init_path
      prompt_on_overwrite  = 'X'
    CHANGING
      filename             = lv_filename
      path                 = lv_path
      fullpath             = lv_fullpath
*     USER_ACTION          =
*     FILE_ENCODING        =
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc = 0.
    pv_fullpath = lv_fullpath.
    pv_path     = lv_path.
  ENDIF.
ENDFORM.
*--------------------------------------------------------------------*
*       下载文件
*--------------------------------------------------------------------*
FORM frm_down  USING pr_filename.
  DATA: lv_objdata     LIKE wwwdatatab,
        lv_mime        LIKE w3mime,
        lv_destination LIKE rlgrap-filename,
        lv_objnam      TYPE string,
        lv_rc          LIKE sy-subrc,
        lv_errtxt      TYPE string.
DATA: lv_filename TYPE string,
        lv_result,
        lv_subrc    TYPE sy-subrc.
DATA: lv_objid TYPE wwwdatatab-objid .
lv_objid = l_muban.  "上传的模版名称
*&---查找文件是否存在。
  SELECT SINGLE relid objid
    FROM wwwdata
    INTO CORRESPONDING FIELDS OF lv_objdata
    WHERE srtf2    = 0
    AND   relid    = 'MI'
  AND   objid    = lv_objid.
*&---判断模版不存在则报错
  IF sy-subrc NE 0 OR lv_objdata-objid EQ space.
    CONCATENATE '模板文件:' lv_objid '不存在,请用TCODE:SMW0进行加载'
    INTO lv_errtxt.
    MESSAGE lv_errtxt TYPE 'E'.
  ENDIF.
lv_filename = pr_filename.
"判断本地地址是否已经存在此文件。
  CALL METHOD cl_gui_frontend_services=>file_exist
    EXPORTING
      file                 = lv_filename
    RECEIVING
      result               = lv_result
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      wrong_parameter      = 3
      not_supported_by_gui = 4
      OTHERS               = 5.
  IF sy-subrc <> 0.
    CASE sy-subrc.
      WHEN 1.
      WHEN 2.
      WHEN OTHERS.
    ENDCASE.
  ENDIF.
IF lv_result EQ 'X'.  "如果存在则删除原始文件,重新覆盖
    CALL METHOD cl_gui_frontend_services=>file_delete
      EXPORTING
        filename             = lv_filename
      CHANGING
        rc                   = lv_subrc
      EXCEPTIONS
        file_delete_failed   = 1
        cntl_error           = 2
        error_no_gui         = 3
        file_not_found       = 4
        access_denied        = 5
        unknown_error        = 6
        not_supported_by_gui = 7
        wrong_parameter      = 8
        OTHERS               = 9.
IF sy-subrc <> 0.
      CASE sy-subrc.
        WHEN 1.
        WHEN 2.
        WHEN OTHERS.
      ENDCASE.
    ENDIF.
IF lv_subrc <> 0. "如果删除失败,则报错。
      CONCATENATE '同名EXCEL文件已打开' '请关闭该EXCEL后重试。'
      INTO lv_errtxt.
      MESSAGE lv_errtxt TYPE 'E'.
    ENDIF.
  ENDIF.
lv_destination   = pr_filename.
*&---下载模版。
  CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
    EXPORTING
      key         = lv_objdata
      destination = lv_destination
    IMPORTING
      rc          = lv_rc.
  IF lv_rc NE 0.
    CONCATENATE '模板文件' '下载失败' INTO lv_errtxt.
    MESSAGE lv_errtxt TYPE 'E'.
  ENDIF.
ENDFORM.
相关推荐
记录成长java25 分钟前
ServletContext,Cookie,HttpSession的使用
java·开发语言·servlet
前端青山25 分钟前
Node.js-增强 API 安全性和性能优化
开发语言·前端·javascript·性能优化·前端框架·node.js
睡觉谁叫~~~29 分钟前
一文解秘Rust如何与Java互操作
java·开发语言·后端·rust
音徽编程30 分钟前
Rust异步运行时框架tokio保姆级教程
开发语言·网络·rust
观音山保我别报错31 分钟前
C语言扫雷小游戏
c语言·开发语言·算法
dsywws1 小时前
Linux学习笔记之vim入门
linux·笔记·学习
小屁孩大帅-杨一凡1 小时前
java后端请求想接收多个对象入参的数据
java·开发语言
m0_656974741 小时前
C#中的集合类及其使用
开发语言·c#
java1234_小锋2 小时前
使用 RabbitMQ 有什么好处?
java·开发语言
晨曦_子画2 小时前
3种最难学习和最容易学习的 3 种编程语言
学习