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.
相关推荐
我曾经是个程序员5 分钟前
鸿蒙学习记录之http网络请求
服务器·学习·http
m0_7482323915 分钟前
WebRTC学习二:WebRTC音视频数据采集
学习·音视频·webrtc
Am心若依旧40932 分钟前
[c++11(二)]Lambda表达式和Function包装器及bind函数
开发语言·c++
明月看潮生34 分钟前
青少年编程与数学 02-004 Go语言Web编程 20课题、单元测试
开发语言·青少年编程·单元测试·编程与数学·goweb
大G哥44 分钟前
java提高正则处理效率
java·开发语言
VBA63371 小时前
VBA技术资料MF243:利用第三方软件复制PDF数据到EXCEL
开发语言
轩辰~1 小时前
网络协议入门
linux·服务器·开发语言·网络·arm开发·c++·网络协议
小_太_阳1 小时前
Scala_【1】概述
开发语言·后端·scala·intellij-idea
向宇it1 小时前
【从零开始入门unity游戏开发之——unity篇02】unity6基础入门——软件下载安装、Unity Hub配置、安装unity编辑器、许可证管理
开发语言·unity·c#·编辑器·游戏引擎
虾球xz2 小时前
游戏引擎学习第55天
学习·游戏引擎