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.
相关推荐
专注VB编程开发20年4 分钟前
无需安装Office进行 Word、Excel操作的微软开发库
microsoft·word·excel
沐土Arvin7 分钟前
Nginx 核心配置详解与性能优化最佳实践
运维·开发语言·前端·nginx·性能优化
weixin_3077791321 分钟前
C#实现HiveQL建表语句中特殊数据类型的包裹
开发语言·数据仓库·hive·c#
難釋懷28 分钟前
JavaScript基础-移动端常用开发插件
开发语言·javascript·ecmascript
阿巴阿巴拉1 小时前
Scala相关知识总结3
开发语言·python
漫天转悠1 小时前
Java8 到 Java21 系列之 Lambda 表达式:函数式编程的开端(Java 8)
java·开发语言
陌言不会python1 小时前
谷粒微服务高级篇学习笔记整理---thymeleaf
笔记·学习·微服务
yiridancan1 小时前
深入浅出:Spring Bean 的初始化流程详解
java·开发语言·后端·spring
laimaxgg1 小时前
数据结构B树的实现
开发语言·数据结构·c++·b树·算法
阳光_你好2 小时前
解决用git bash终端 tail -f 命令查看日志中文乱码问题
开发语言·git·bash