Make used of OLE to create a new Excel File
*
* Make used of OLE to create a new Excel File.
*
* Written by : SAP Basis, ABAP Programming and Other IMG Stuff
*              http://www.erpgreat.com
*

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

Fast Links:
BDC program examples:
Auto Disallowed Back Posting to Previous Period

Get help for your ABAP problems
Do you have a ABAP Question?

SAP Books
SAP Certification, Functional, Basis Administration and ABAP Programming Reference Books

ABAP Programming Tips
ABAP and Samples Program Codes for Abapers

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.