Export an excel file with inserting an image

*&———————————————————————*
*& Report ZRIC_OLE2
*&———————————————————————*
*&
*&———————————————————————*
REPORT ZRIC_OLE2.
TYPE-POOLS: SOI,OLE2.
DATA: LO_APPLICATION TYPE OLE2_OBJECT,
LO_WORKBOOK TYPE OLE2_OBJECT,
LO_WORKBOOKS TYPE OLE2_OBJECT,
LO_RANGE TYPE OLE2_OBJECT,
LO_WORKSHEET TYPE OLE2_OBJECT,
LO_WORKSHEETS TYPE OLE2_OBJECT,
LO_COLUMN TYPE OLE2_OBJECT,
LO_ROW TYPE OLE2_OBJECT,
LO_CELL TYPE OLE2_OBJECT,
LO_CELLS TYPE OLE2_OBJECT,
LO_FONT TYPE OLE2_OBJECT.
DATA: LO_CELLSTART TYPE OLE2_OBJECT,
LO_CELLEND TYPE OLE2_OBJECT,
LO_SELECTION TYPE OLE2_OBJECT,
LO_VALIDATION TYPE OLE2_OBJECT.

DATA: LV_SELECTED_FOLDER TYPE STRING,
LV_COMPLETE_PATH TYPE CHAR256,
LV_TITULO TYPE STRING.
DATA LO_PICTURE TYPE OLE2_OBJECT.

CALL METHOD CL_GUI_FRONTEND_SERVICES=>DIRECTORY_BROWSE
EXPORTING
WINDOW_TITLE = LV_TITULO
INITIAL_FOLDER = ‘C:\’
CHANGING
SELECTED_FOLDER = LV_SELECTED_FOLDER
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
OTHERS = 3.
CHECK NOT LV_SELECTED_FOLDER IS INITIAL.

CREATE OBJECT LO_APPLICATION ‘Excel.Application’.
CALL METHOD OF LO_APPLICATION ‘Workbooks’ = LO_WORKBOOKS.
CALL METHOD OF LO_WORKBOOKS ‘Add’ = LO_WORKBOOK.
SET PROPERTY OF LO_APPLICATION ‘Visible’ = 0.
GET PROPERTY OF LO_APPLICATION ‘ACTIVESHEET’ = LO_WORKSHEET.
* ———-
* —- PASTE HERE THE CODE
* ———-

CALL METHOD OF LO_APPLICATION ‘RANGE’ = LO_CELL EXPORTING #1 = ‘A1’.
SET PROPERTY OF LO_CELL ‘VALUE’ = ‘Hello World !!!’ .
CALL METHOD OF LO_APPLICATION ‘RANGE’ = LO_CELL EXPORTING #1 = ‘B1’.
SET PROPERTY OF LO_CELL ‘VALUE’ = ‘My name is Henry.’ .

CALL METHOD OF LO_WORKSHEET ‘Range’ = LO_CELLS

EXPORTING

#1 = ‘F1’

#2 = ‘F2’.

CALL METHOD OF LO_CELLS ‘Select’ = LO_SELECTION.

GET PROPERTY OF LO_WORKSHEET ‘Pictures’ = LO_PICTURE.

CALL METHOD OF LO_PICTURE ‘Insert’
EXPORTING
#1 = ‘D:\tmp\logo.bmp’.

CONCATENATE LV_SELECTED_FOLDER ‘\Test’ INTO LV_COMPLETE_PATH.

CALL METHOD OF LO_WORKBOOK ‘SaveAs’
EXPORTING
#1 = LV_COMPLETE_PATH.
IF SY-SUBRC EQ 0.
MESSAGE ‘File downloaded successfully’ TYPE ‘S’.
ELSE.
MESSAGE ‘Error downloading the file’ TYPE ‘E’.
ENDIF.

CALL METHOD OF LO_APPLICATION ‘QUIT’.
FREE OBJECT LO_WORKSHEET.
FREE OBJECT LO_WORKBOOK.
FREE OBJECT LO_APPLICATION.

10 thoughts on “Export an excel file with inserting an image

Leave a Reply

Your email address will not be published. Required fields are marked *