SAP ABAP excel文件模板上传下载

一:事物码smw0

二:上传步骤

SAP ABAP excel文件模板上传下载_第1张图片SAP ABAP excel文件模板上传下载_第2张图片SAP ABAP excel文件模板上传下载_第3张图片SAP ABAP excel文件模板上传下载_第4张图片

程序源码:

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.

你可能感兴趣的:(SAP,ABAP,excel,SAP,ABAP,开发语言,学习)