Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
RichHeilman
Developer Advocate
Developer Advocate

I recently got involved with a project where the user wanted to update an existing Excel spreadsheet automatically via an ABAP program.  I started digging in and found that this could be accomplished using OLE technology.   This weblog gives an small example application which any beginner could use to get their application started.

ABAP Source Code

report zole_example. 

include ole2incl.

data: e_sheet type ole2_object.

data: e_appl  type ole2_object.

data: e_work  type ole2_object.

data: e_cell  type ole2_object. 

data: field_value(30) type c. 

parameters: p_file type localfile default 'C:RichTest.xls'.

start-of-selection. 

* Start the application  

create object e_appl 'EXCEL.APPLICATION'.  

set property of e_appl 'VISIBLE' = 1.

* Open the file 

call method of e_appl 'WORKBOOKS' = e_work.   

call method of e_work 'OPEN'  

        exporting   #1 = p_file.

* Write data to the excel file

  do 20 times.

* Create the value  

  field_value  = sy-index.    

  shift field_value left deleting leading space.    

  concatenate 'Cell' field_value  into field_value separated by space. 

* Position to specific cell  in  Column 1    

  call method of e_appl 'Cells' = e_cell  exporting  #1 = sy-index  #2 = 1.

* Set the value  

  set property of e_cell 'Value' = field_value . 

* Position to specific cell  in  Column 2   

  call method of e_appl 'Cells' = e_cell  exporting  #1 = sy-index  #2 = 2.

* Set the value   

  set property of e_cell 'Value' = field_value .

  enddo. 

* Close the file  

  call method of e_work 'close'.

* Quit the file 

  call method of  e_appl  'QUIT'. 

  free object e_appl.

Result

 

For more information on ABAP/OLE,  search on "OLE" in the ABAP forum and check out this great article called An Easy Reference For OLE Automation by Serdar Simsekler.  This guide will go over more advanced topics and has a lot more code samples.

20 Comments