ABAP - OLE excel download

FORM alv_user_command USING r_ucomm LIKE sy-ucomm
                            rs_selfield TYPE slis_selfield.

   CASE r_ucomm.

    WHEN 'DOWN'.
      PERFORM frm_ole_down.

 

FORM frm_ole_down .

  PERFORM frm_get_model.
  PERFORM frm_fill_cell.

 

FORM frm_get_model .

  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      default_extension    = 'XLS'
      default_file_name    = '公司历史科目余额清册'
    CHANGING
      filename             = lc_filename
      path                 = lc_path
      fullpath             = lc_fullpath
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF lc_fullpath = ''.
    EXIT.
  ENDIF.

  SELECT SINGLE relid objid
    INTO CORRESPONDING FIELDS OF l_para
    FROM wwwdata
   WHERE wwwdata~relid = 'MI'
     AND wwwdata~objid = 'ZFIR45'
     AND wwwdata~srtf2 = 0.

  IF sy-subrc <> 0.
    MESSAGE '下载模板未找到' TYPE 'S' DISPLAY LIKE 'E'.     "The excel template  no existed !
    EXIT.
  ENDIF.
  ls_destination = lc_fullpath.
  CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
    EXPORTING
      key         = l_para
      destination = ls_destination
    IMPORTING
      rc          = li_rc.
    IF li_rc NE 0.
    MESSAGE '模板下载过程出错' TYPE 'S' DISPLAY LIKE 'E'.     "The excel template download unsuccessfully !
    EXIT.
  ENDIF.

 

FORM frm_fill_cell .
  local_file = ls_destination.

  CREATE OBJECT appl 'EXCEL.APPLICATION'.
  IF sy-subrc NE 0.
    EXIT.
  ENDIF.

  SET PROPERTY OF appl 'VISIBLE' = 0.
  SET PROPERTY OF appl 'DisplayAlerts' = 0.
  CALL METHOD OF appl 'WORKBOOKS' = book.

  CALL METHOD OF book 'OPEN' EXPORTING #1 = local_file.

  CALL METHOD OF appl 'WORKSHEETS' = sheet EXPORTING #1 = 1.

  CALL METHOD OF sheet 'SELECT'.         "*填充抬头


  DATA:l_index TYPE i VALUE 1.
  DATA:l_pzdat(8).
  LOOP AT gt_out_tmp.
    l_index = l_index + 1.
    marco1   l_index  1  gt_out_tmp-flag  .

    l_pzdat = gt_out_tmp-pzdat.
    marco1  l_index 2 l_pzdat  .
    CLEAR l_pzdat.

    marco1  l_index 3 gt_out_tmp-pztyp  .
    marco1  l_index 4 gt_out_tmp-bukrs  .
    marco1  l_index 5 gt_out_tmp-pzpot  .
    marco1  l_index 6 gt_out_tmp-pzpet  .
    marco1  l_index 7 gt_out_tmp-ckao0  .
    marco1  l_index 8 gt_out_tmp-waers  .
    marco1  l_index 9 gt_out_tmp-dhrat  .
    marco1  l_index 10  gt_out_tmp-newbs  .
    marco1  l_index 11  gt_out_tmp-hkont_o  .
    marco1  l_index 12  gt_out_tmp-rstgr  .
    marco1  l_index 13  gt_out_tmp-amtbl  .
    marco1  l_index 14  gt_out_tmp-knref  .
    marco1  l_index 15  gt_out_tmp-zzname2  .
    marco1  l_index 16  gt_out_tmp-umskz  .
    marco1  l_index 17  gt_out_tmp-dmbtr  .
    marco1  l_index 18  gt_out_tmp-ddmon  .
    marco1  l_index 19  gt_out_tmp-ckao1  .
    marco1  l_index 20  gt_out_tmp-ckao2  .
    marco1  l_index 21  gt_out_tmp-ckao3  .
    marco1  l_index 22  gt_out_tmp-kostl  .
    marco1  l_index 23  gt_out_tmp-kunnr  .
    marco1  l_index 24  gt_out_tmp-matnr1 .
    marco1  l_index 25  gt_out_tmp-waymat .
    marco1  l_index 26  gt_out_tmp-kwmeng .
    marco1  l_index 27  gt_out_tmp-meins  .
    marco1  l_index 28  gt_out_tmp-vkorg  .
    marco1  l_index 29  gt_out_tmp-vtweg  .
    marco1  l_index 30  gt_out_tmp-spart  .
    marco1  l_index 31  gt_out_tmp-kukla  .
    marco1  l_index 32  gt_out_tmp-vkbur  .
    marco1  l_index 33  gt_out_tmp-salgr  .
    marco1  l_index 34  gt_out_tmp-httyp  .
    marco1  l_index 35  gt_out_tmp-fkdat  .
    marco1  l_index 36  gt_out_tmp-jzdat  .
    marco1  l_index 37  gt_out_tmp-smark  .
    marco1  l_index 38  gt_out_tmp-fpzd .
    marco1  l_index 39  gt_out_tmp-text1  .
    marco1  l_index 40  gt_out_tmp-smony  .
    marco1  l_index 41  gt_out_tmp-vbeln  .
    marco1  l_index 42  gt_out_tmp-werks  .
    marco1  l_index 43  gt_out_tmp-bwtar  .
    marco1  l_index 44  gt_out_tmp-title  .
    marco1  l_index 45  gt_out_tmp-partner  .
    CLEAR gt_out_tmp.
  ENDLOOP.
  IF sy-subrc = 0.
    MESSAGE '模板下载完成' TYPE 'S'.
  ENDIF.

  CALL METHOD OF appl 'SAVE'.                      "保存
  CALL METHOD OF appl 'Quit'.                      "退出
  GET PROPERTY OF appl  'ACTIVESHEET'    = sheet.
  GET PROPERTY OF appl  'ACTIVEWORKBOOK' = book.
  SET PROPERTY OF sheet 'NAME'           = '1'.

  FREE OBJECT range.
  FREE OBJECT sheet.
  FREE OBJECT workbook.
  FREE OBJECT appl.

 

DEFINE marco1.
  call method of appl 'CELLS' = cell
    exporting
      #1 = &1
      #2 = &2.
  set property of cell 'VALUE' = &3.
END-OF-DEFINITION.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值