SAP中使用OLE2对象的两个样例程序

样例一、

REPORT  ZSAPLINK_INSTALLER_TEMPLATE.
TABLES sscrfields.

TYPE-POOLS  ole2.

DATA: excel TYPE ole2_object,
      word  TYPE ole2_object,
      book  TYPE ole2_object,
      rc    TYPE c LENGTH 8.

SELECTION-SCREEN:
  BEGIN OF SCREEN 100 AS WINDOW TITLE title,
    BEGIN OF LINE,
      PUSHBUTTON  2(12) button_1
                 USER-COMMAND word_start,
      PUSHBUTTON  20(12) button_2
                 USER-COMMAND excel_start,
    END OF LINE,
    BEGIN OF LINE,
      PUSHBUTTON  2(12) button_3
                  USER-COMMAND word_stop,
      PUSHBUTTON  20(12) button_4
                 USER-COMMAND excel_stop,
    END OF LINE,
  END OF SCREEN 100.

START-OF-SELECTION.
  button_1 = 'Start Word'.
  button_2 = 'Start Excel'.
  button_3 = 'Stop  Word'.
  button_4 = 'Stop  Excel'.
  CALL SELECTION-SCREEN 100 STARTING AT 10 10.

AT SELECTION-SCREEN.
  CASE sscrfields-ucomm.
    WHEN 'WORD_START'.
      CHECK word-handle <> -1.
      CHECK word-header = space.
      CREATE OBJECT   word  'Word.Basic'.
      CALL METHOD  OF word  'AppShow'.
    WHEN 'EXCEL_START'.
      CHECK excel-handle = 0.
      CHECK excel-header = space.
      CREATE OBJECT   excel 'Excel.Application'.
      SET PROPERTY OF excel 'Visible' = 1.
      GET PROPERTY OF excel 'Workbooks' = book.
      CALL METHOD  OF book  'Open' = rc
        EXPORTING #1 = 'C:/temp/Table.xls'.
    WHEN 'WORD_STOP'.
      CALL METHOD OF word 'AppClose'.
      FREE OBJECT word.
      CLEAR: word-handle, word-header.
    WHEN 'EXCEL_STOP'.
      CALL METHOD OF  excel 'Quit'.
      FREE OBJECT excel.
      CLEAR: excel-handle, excel-header.
    WHEN OTHERS.
      LEAVE PROGRAM.
  ENDCASE. 

样例二

REPORT ZEXCEL NO STANDARD PAGE HEADING.
INCLUDE OLE2INCL.
DATA: EXCEL_OBJECT TYPE OLE2_OBJECT, “ EXCEL OBJECT
WORKBOOK_LIST TYPE OLE2_OBJECT, “ LIST OF WORKBOOKS
WORKBOOK TYPE OLE2_OBJECT, “ WORKBOOK
CELL TYPE OLE2_OBJECT, “ CELL
FONT TYPE OLE2_OBJECT. “ FONT
TABLES: PA0002.
DATA ROW_NUMBER TYPE I.
DATA: IT_PA0002 LIKE PA0002 OCCURS 10 WITH HEADER LINE.
START-OF-SELECTION.
SELECT * FROM PA0002 INTO TABLE IT_PA0002 UP TO 10 ROWS.
CREATE OBJECT EXCEL_OBJECT ‘EXCEL.APPLICATION’.
SET PROPERTY OF EXCEL_OBJECT ‘VISIBLE’ = 1.
CALL METHOD OF EXCEL_OBJECT ‘WORKBOOKS’ = WORKBOOK_LIST.
CALL METHOD OF WORKBOOK_LIST ‘ADD’ = WORKBOOK.
* FILL THE ACTIVE SHEET WITH COLUMN HEADINGS
PERFORM FILL_CELL_WITH_DATA USING 1 1 1 ‘Employee Number’.
PERFORM FILL_CELL_WITH_DATA USING 1 2 1 ‘First Name’.
PERFORM FILL_CELL_WITH_DATA USING 1 3 1 ‘Last Name’.
PERFORM FILL_CELL_WITH_DATA USING 1 4 1 ‘Date of Birth’.
PERFORM FILL_CELL_WITH_DATA USING 1 5 1 ‘Place of Birth’.
* FILL EMPLOYEE DATA TO ACTIVE EXCEL SHEET
LOOP AT IT_PA0002.
ROW_NUMBER = SY-TABIX + 1.
PERFORM FILL_CELL_WITH_DATA USING ROW_NUMBER 1 0 IT_PA0002-PERNR.
PERFORM FILL_CELL_WITH_DATA USING ROW_NUMBER 2 0 IT_PA0002-VORNA.
PERFORM FILL_CELL_WITH_DATA USING ROW_NUMBER 3 0 IT_PA0002-NACHN.
PERFORM FILL_CELL_WITH_DATA USING ROW_NUMBER 4 0 IT_PA0002-GBDAT.
PERFORM FILL_CELL_WITH_DATA USING ROW_NUMBER 5 0 IT_PA0002-GBORT.
ENDLOOP.
FREE OBJECT EXCEL_OBJECT.
FORM FILL_CELL_WITH_DATA USING ROW COLUMN BOLD VAL.
CALL METHOD OF EXCEL_OBJECT
‘CELLS’ = CELL
EXPORTING #1 = ROW
#2 = COLUMN.
SET PROPERTY OF CELL ‘VALUE’ = VAL.
GET PROPERTY OF CELL ‘FONT’ = FONT.
SET PROPERTY OF FONT ‘BOLD’ = BOLD.
ENDFORM.

 使用OLE的程序框架模板
…………………………
INCLUDE OLE2INCL.
DATA: EXCEL_OBJECT TYPE OLE2_OBJECT,
WORKBOOK_LIST TYPE OLE2_OBJECT.
…………………………
CREATE OBJECT EXCEL_OBJECT ‘EXCEL.APPLICATION’.
CALL METHOD OF EXCEL_OBJECT ‘WORKBOOKS’ = WORKBOOK_LIST.
CALL METHOD OF WORKBOOK_LIST ‘ADD’ = WORKBOOK.
…………………………
…………………………
SET PROPERTY OF CELL ‘VALUE’ = VAL.
…………………………
…………………………
FREE OBJECT EXCEL_OBJECT.
…………………………
…………………………

相关链接
http://blog.csdn.net/CompassButton/archive/2006/11/23/1408409.aspx

posted on 2007-02-28 15:17  毛小娃  阅读(215)  评论(0编辑  收藏  举报

导航