excel操作1

 1. SMW0 EXCEL模板下载

REFERENCE(IV_NAME) TYPE  W3OBJID
*"     REFERENCE(IV_DEF_FILENAME) TYPE  CHAR255 OPTIONAL
*"----------------------------------------------------------------------

  data: lv_odat type wwwdatatab,
        lv_mstr type string,
        lv_path type rlgrap-filename,
        lv_sbrc type sy-subrc.
  clear: lv_path, lv_odat.
  call function 'WS_FILENAME_GET'   " 保存用户选择的路径 --
    exporting
      def_path         = 'E:\'
      mask             = ',Excel file,*.xls;*.xlsx;' " 限制为Excel文件 --
      mode             = 'S'
      title            = '下载Excel模版……'
      DEF_FILENAME     = IV_DEF_FILENAME
    importing
      filename         = lv_path                     " 返回路径及文件名 --
    exceptions
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      others           = 5.
  check lv_path is not initial.
  select single relid objid into corresponding fields of lv_odat  " 下载模版 --
    from wwwdata where srtf2 eq 0 and relid eq 'MI' and objid eq iv_name.
  if ( sy-subrc ne 0 ) or ( lv_odat-objid is initial ).
    concatenate '模板文件:' iv_name '不存在,请用TCODE:SMW0进行加载' into lv_mstr.
    message lv_mstr type 'E'.  exit.
  endif.
  call function 'DOWNLOAD_WEB_OBJECT'
    exporting
      key         = lv_odat
      destination = lv_path
    importing
      rc          = lv_sbrc.
  if lv_sbrc ne 0.
    write : / '模板文件:' , iv_name, '下载失败,请与开发人员联系'.
  endif.
  perform open_excel using lv_path.

*----------------------------------------------------------------------*
*       FORM open_excel                            Text: 打开Excel模版 *
*----------------------------------------------------------------------*
form open_excel using iv_name type rlgrap-filename.
  data: lo_excel type ole2_object,
        lo_book  type ole2_object.
  check iv_name is not initial.
  create object lo_excel 'Excel.Application'.
  set property of lo_excel 'Visible' = 1.
  call method of lo_excel 'Workbooks' = lo_book.
  call method of lo_book 'Open' = lo_book
    exporting #1 = iv_name.
  if sy-subrc ne 0.
    message e899(mm) with '打开Excel模板失败!'.
  endif.
  free: lo_excel, lo_book.
endform. 

 2. 打开上传EXCEL模板

*----------------------------------------------------------------------*
*       FORM upld_excel                            Text: 打开Excel文件 *
*----------------------------------------------------------------------*
form upld_excel changing cv_name type rlgrap-filename.
  data: lv_indx type i,
        lv_uact type i,
        lv_filt type string,
        lt_file type filetable,
        ls_file type file_table.

  clear: lv_indx, lv_uact, lv_filt, ls_file.  refresh: lt_file.
  concatenate cl_gui_frontend_services=>filetype_excel
              cl_gui_frontend_services=>filetype_all '|' into lv_filt.

  call method cl_gui_frontend_services=>file_open_dialog
    exporting
      window_title            =  '请选择您要上传的文件'
      default_extension       = cl_gui_frontend_services=>filetype_excel
      default_filename        = 'Default.XLS'
      file_filter             = lv_filt
      with_encoding           = space
*      initial_directory       = lv_path
      multiselection          = space
    changing
      file_table              = lt_file
      rc                      = lv_indx
      user_action             = lv_uact
    exceptions
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      others                  = 5
          .
  if sy-subrc ne 0.
    message id sy-msgid type sy-msgty number sy-msgno
               with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.
  if lv_uact eq cl_gui_frontend_services=>action_ok .
    read table lt_file index lv_indx into ls_file.
    cv_name = ls_file-filename.
  endif.
endform.          " upld_excel  

  • 23
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值