메뉴 건너뛰기

SAP 한국 커뮤니티

SAMPLE OLE 프로그램

sapjoy 2006.12.20 23:57 조회 수 : 6112 추천:47

REPORT  Z11_003                                                     .
INCLUDE OLE2INCL.
* Tables and variables for later use
TABLES: CUSTOMERS.
DATA: APPLICATION TYPE OLE2_OBJECT,
      WORKBOOK    TYPE OLE2_OBJECT,
      SHEET       TYPE OLE2_OBJECT,
      CELLS       TYPE OLE2_OBJECT.
* Creating an object
CREATE OBJECT APPLICATION 'excel.application'.
IF SY-SUBRC NE 0.
   WRITE: / 'Error when opening excel.application', SY-MSGLI.
ENDIF.
* Setting properties
SET PROPERTY OF APPLICATION 'Visible' = 1.
* Calling methods
CALL METHOD OF APPLICATION 'Workbooks' = WORKBOOK.
PERFORM ERRORS.
CALL METHOD OF WORKBOOK 'Add'.
PERFORM ERRORS.
CALL METHOD OF APPLICATION 'Worksheets' = SHEET EXPORTING #1 = 1.
PERFORM ERRORS.
CALL METHOD OF SHEET 'Activate'.
PERFORM ERRORS.
PERFORM FILL_SHEET.

* Subroutine for filling the spread sheet
FORM FILL_SHEET.
  DATA: ROW_MAX TYPE I VALUE 256,
        INDEX   TYPE I.
  FIELD-SYMBOLS: <NAME>.
  SELECT * FROM CUSTOMERS.
    INDEX = ROW_MAX * ( SY-DBCNT - 1 ) + 1.
    DO 4 TIMES.
      ASSIGN COMPONENT SY-INDEX OF STRUCTURE CUSTOMERS TO <NAME>.
      CALL METHOD OF SHEET 'Cells' = CELLS
                           EXPORTING #1 = INDEX.
      SET PROPERTY OF CELLS 'Value' = <NAME>.
      ADD 1 TO INDEX.
    ENDDO.
  ENDSELECT.
ENDFORM.
* Subroutine for error handling
FORM ERRORS.
  IF SY-SUBRC NE 0.
    WRITE: / 'Error in OLE call', SY-MSGLI.
    EXIT.
  ENDIF.
ENDFORM.