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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
23 | |
5 | |
4 | |
3 | |
2 | |
2 | |
2 | |
2 | |
2 | |
2 |