Friday, November 23, 2007

Make used of OLE to create a new Excel File in sap abap

REPORT ZCREATEEXCEL.

TYPE-POOLS OLE2.
DATA: EXCEL TYPE OLE2_OBJECT,
WORKBOOKS TYPE OLE2_OBJECT,
WORKBOOK TYPE OLE2_OBJECT.

DATA: FILENAME LIKE RLGRAP-FILENAME.

* START THE EXCEL APPLICATION
CREATE OBJECT EXCEL 'EXCEL.APPLICATION'.
PERFORM ERR_HDL.

* PUT EXCEL IN FRONT
SET PROPERTY OF EXCEL 'VISIBLE' = 1.
PERFORM ERR_HDL.

* INFORM USER OF THE CURRENT STATUS
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
PERCENTAGE = 0
TEXT = TEXT-I08
EXCEPTIONS
OTHERS = 1.

* CREATE AN EXCEL WORKBOOK OBJECT
CALL METHOD OF EXCEL 'WORKBOOKS' = WORKBOOKS.
PERFORM ERR_HDL.

CALL METHOD OF WORKBOOKS 'ADD' = WORKBOOK.
PERFORM ERR_HDL.

* EXCEL FILENAME
CONCATENATE SY-REPID '_' SY-DATUM+6(2) '_' SY-DATUM+4(2) '_'
SY-DATUM(4) '_' SY-UZEIT '.XLS' INTO FILENAME.

CALL METHOD OF WORKBOOK 'SAVEAS' EXPORTING #1 = FILENAME.

FORM ERR_HDL.

IF SY-SUBRC <> 0.
WRITE: / 'OLE ERROR: RETURN CODE ='(I10), SY-SUBRC.
STOP.
ENDIF.

ENDFORM.

*-- End of Program

No comments:

Blog Archive