ABAP--通过OLE操作OFFICE(代码)

本代码是通过OLE技术操作EXCEL,将SAP的数据填写到本地EXCEL中,Office运行在SAP的GUI界面外,注意红色代码。
REPORT ZRSDEMO01 NO STANDARD PAGE HEADING.
* this report demonstrates how to send some ABAP data to an
* EXCEL sheet using OLE automation.
*必须包含
INCLUDE OLE2INCL.
* handles for OLE objects
*申明相关的OFFICE的OLE对象
DATA: H_EXCEL TYPE OLE2_OBJECT,        " Excel object
      H_MAPL TYPE OLE2_OBJECT,         " list of workbooks
      H_MAP TYPE OLE2_OBJECT,          " workbook
      H_ZL TYPE OLE2_OBJECT,           " cell
      H_F TYPE OLE2_OBJECT.            " font
TABLES: SPFLI.
DATA  H TYPE I.
* table of flights
DATA: IT_SPFLI LIKE SPFLI OCCURS 10 WITH HEADER LINE.

*&---------------------------------------------------------------------*
*&   Event START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
* read flights
  SELECT * FROM SPFLI INTO TABLE IT_SPFLI UP TO 10 ROWS.
* tell user what is going on
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
     EXPORTING
*           PERCENTAGE = 0
           TEXT       = TEXT-007
       EXCEPTIONS
            OTHERS     = 1.
* start Excel(创建对象)
  CREATE OBJECT H_EXCEL 'EXCEL.APPLICATION'.
  PERFORM ERR_HDL.
*设置office对象是否显示
  SET PROPERTY OF H_EXCEL  'Visible' = 1.
  PERFORM ERR_HDL.
* tell user what is going on
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
     EXPORTING
*           PERCENTAGE = 0
           TEXT       = TEXT-008
       EXCEPTIONS
            OTHERS     = 1.
* get list of workbooks, initially empty
  CALL METHOD OF H_EXCEL 'Workbooks' = H_MAPL.
  PERFORM ERR_HDL.
* add a new workbook
  CALL METHOD OF H_MAPL 'Add' = H_MAP.
  PERFORM ERR_HDL.
* tell user what is going on
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
     EXPORTING
*           PERCENTAGE = 0
           TEXT       = TEXT-009
       EXCEPTIONS
            OTHERS     = 1.
* output column headings to active Excel sheet
  PERFORM FILL_CELL USING 1 1 1 'Flug'(001).
  PERFORM FILL_CELL USING 1 2 1 'Nr'(002).
  PERFORM FILL_CELL USING 1 3 1 'Von'(003).
  PERFORM FILL_CELL USING 1 4 1 'Nach'(004).
  PERFORM FILL_CELL USING 1 5 1 'Zeit'(005).
  LOOP AT IT_SPFLI.
* copy flights to active EXCEL sheet
    H = SY-TABIX + 1.
    PERFORM FILL_CELL USING H 1 0 IT_SPFLI-CARRID.
    PERFORM FILL_CELL USING H 2 0 IT_SPFLI-CONNID.
    PERFORM FILL_CELL USING H 3 0 IT_SPFLI-CITYFROM.
    PERFORM FILL_CELL USING H 4 0 IT_SPFLI-CITYTO.
    PERFORM FILL_CELL USING H 5 0 IT_SPFLI-DEPTIME.
  ENDLOOP.
* disconnect from Excel
*
  CALL METHOD OF    H_EXCEL  'QUIT'.
  FREE OBJECT H_EXCEL.
  PERFORM ERR_HDL.

*---------------------------------------------------------------------*
*       FORM FILL_CELL                                                *
*---------------------------------------------------------------------*
*       sets cell at coordinates i,j to value val boldtype bold       *
*---------------------------------------------------------------------*
FORM FILL_CELL USING I J BOLD VAL.
*get cell
  CALL METHOD OF H_EXCEL 'Cells' = H_ZL EXPORTING #1 = I #2 = J.
  PERFORM ERR_HDL.
*set cell value
  SET PROPERTY OF H_ZL 'Value' = VAL .
  PERFORM ERR_HDL.
*Get cell font
  GET PROPERTY OF H_ZL 'Font' = H_F.
  PERFORM ERR_HDL.
*set cell font 
  SET PROPERTY OF H_F 'Bold' = BOLD .
  PERFORM ERR_HDL.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  ERR_HDL
*&---------------------------------------------------------------------*
*       outputs OLE error if any                                       *
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM ERR_HDL.
IF SY-SUBRC <> 0.
  WRITE: / 'Fehler bei OLE-Automation:'(010), SY-SUBRC.
  STOP.
ENDIF.
ENDFORM.                    " ERR_HDL
相关链接:http://blog.csdn.net/CompassButton/archive/2007/07/26/1709383.aspx
                   http://blog.csdn.net/CompassButton/archive/2007/03/09/1524959.aspx

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值