Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Creating Excel

Former Member
0 Likes
752

Dear All,

I am using the following code to create one excel sheet in ABAP Screen. Though its showing me the excel sheet, but the data is not getting populated in the excel sheet. Kindly help ASAP.

*Populating Internal Table

SELECT vbeln matnr erdat UP TO 10 ROWS

FROM vbap

INTO TABLE t_itab.

*" Create the instance CONTROL

CALL METHOD c_oi_container_control_creator=>get_container_control

IMPORTING

control = control

error = error.

*" Create a container

CREATE OBJECT container

EXPORTING container_name = 'CONTAINER'.

*" create the object that manages the link to the OLE interface

CALL METHOD control->init_control

EXPORTING

r3_application_name = 'R/3 Basis'

inplace_enabled = inplace

inplace_scroll_documents = 'X'

parent = container

register_on_close_event = 'X'

register_on_custom_event = 'X'

no_flush = 'X'

IMPORTING

error = error.

CALL METHOD control->get_document_proxy

EXPORTING

document_type = 'Excel.Sheet'

IMPORTING

document_proxy = document

error = error.

CALL METHOD document->create_document

EXPORTING

document_title = 'Excel'

open_inplace = 'X'.

CALL METHOD document->has_spreadsheet_interface

IMPORTING

is_available = has.

CALL METHOD document->get_spreadsheet_interface

IMPORTING

sheet_interface = handle.

CALL METHOD handle->insert_full

EXPORTING

data = t_itab[]

sema = lt_sema[]

hkey = lt_hkey[]

vkey = lt_vkey[]

online_text = lt_online[]

n_vrt_keys = 0

n_hrz_keys = 0

n_att_cols = l_colno

EXCEPTIONS

dim_mismatch_data = 1

dim_mismatch_sema = 2

dim_mismatch_vkey = 3

error_in_hkey = 4

error_in_vkey = 5

inv_data_range = 6

error_in_sema = 7

OTHERS = 8.

IF sy-subrc NE 0.

MESSAGE e001(38) WITH 'Error Code: ' sy-subrc.

ENDIF.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
673

hi use this....

Report ZMULTICOLOR_TEST no standard page heading.

  • this report demonstrates how to send some ABAP data to an

  • EXCEL sheet using OLE automation.

include ole2incl.

  • handles for OLE objects

data: h_excel type ole2_object, " Excel object

h_mapl type ole2_object, " list of workbooks

h_map type ole2_object, " workbook

h_zl type ole2_object, " cell

h_f type ole2_object, " font

h_c type ole2_object. " color

DATA: FILENAME LIKE RLGRAP-FILENAME.

tables: spfli.

data h type i.

  • table of flights

data: it_spfli like spfli occurs 10 with header line.

&----


*& Event START-OF-SELECTION

&----


start-of-selection.

  • read flights

select * from spfli into table it_spfli.

  • display header

uline (61).

write: / sy-vline no-gap,

(3) 'Flg'(001) color col_heading no-gap, sy-vline no-gap,

(4) 'Nr'(002) color col_heading no-gap, sy-vline no-gap,

(20) 'Von'(003) color col_heading no-gap, sy-vline no-gap,

(20) 'Nach'(004) color col_heading no-gap, sy-vline no-gap,

(8) 'Zeit'(005) color col_heading no-gap, sy-vline no-gap.

uline /(61).

  • display flights

loop at it_spfli.

write: / sy-vline no-gap,

it_spfli-carrid color col_key no-gap, sy-vline no-gap,

it_spfli-connid color col_normal no-gap, sy-vline no-gap,

it_spfli-cityfrom color col_normal no-gap, sy-vline no-gap,

it_spfli-cityto color col_normal no-gap, sy-vline no-gap,

it_spfli-deptime color col_normal no-gap, sy-vline no-gap.

endloop.

uline /(61).

  • tell user what is going on

call function 'SAPGUI_PROGRESS_INDICATOR'

exporting

  • PERCENTAGE = 0

text = text-007

exceptions

others = 1.

  • start Excel

create object h_excel 'EXCEL.APPLICATION'.

  • PERFORM ERR_HDL.

set property of h_excel 'Visible' = 1.

  • CALL METHOD OF H_EXCEL 'FILESAVEAS' EXPORTING #1 = 'c:\kis_excel.xls' .

  • PERFORM ERR_HDL.

  • tell user what is going on

call function 'SAPGUI_PROGRESS_INDICATOR'

exporting

  • PERCENTAGE = 0

text = text-008

exceptions

others = 1.

  • get list of workbooks, initially empty

call method of h_excel 'Workbooks' = h_mapl.

perform err_hdl.

  • add a new workbook

call method of h_mapl 'Add' = h_map.

perform err_hdl.

  • tell user what is going on

call function 'SAPGUI_PROGRESS_INDICATOR'

exporting

  • PERCENTAGE = 0

text = text-009

exceptions

others = 1.

  • output column headings to active Excel sheet

perform fill_cell using 1 1 1 200 'Carrier id'(001).

perform fill_cell using 1 2 1 200 'Connection id'(002).

perform fill_cell using 1 3 1 200 'City from'(003).

perform fill_cell using 1 4 1 200 'City to'(004).

perform fill_cell using 1 5 1 200 'Dep. Time'(005).

loop at it_spfli.

  • copy flights to active EXCEL sheet

h = sy-tabix + 1.

if it_spfli-carrid cs 'AA'.

perform fill_cell using h 1 0 000255000 it_spfli-carrid.

elseif it_spfli-carrid cs 'AZ'.

perform fill_cell using h 1 0 168000000 it_spfli-carrid.

elseif it_spfli-carrid cs 'JL'.

perform fill_cell using h 1 0 168168000 it_spfli-carrid.

elseif it_spfli-carrid cs 'LH'.

perform fill_cell using h 1 0 111111111 it_spfli-carrid.

elseif it_spfli-carrid cs 'SQ'.

perform fill_cell using h 1 0 100100100 it_spfli-carrid.

else.

perform fill_cell using h 1 0 000145000 it_spfli-carrid.

endif.

if it_spfli-connid lt 400.

perform fill_cell using h 2 0 255000255 it_spfli-connid.

elseif it_spfli-connid lt 800.

perform fill_cell using h 2 0 077099088 it_spfli-connid.

else.

perform fill_cell using h 2 0 246156138 it_spfli-connid.

endif.

if it_spfli-cityfrom cp 'S*'.

perform fill_cell using h 3 0 155155155 it_spfli-cityfrom.

elseif it_spfli-cityfrom cp 'N*'.

perform fill_cell using h 3 0 189111222 it_spfli-cityfrom.

else.

perform fill_cell using h 3 0 111230222 it_spfli-cityfrom.

endif.

if it_spfli-cityto cp 'S*'.

perform fill_cell using h 4 0 200200200 it_spfli-cityto.

elseif it_spfli-cityto cp 'N*'.

perform fill_cell using h 4 0 000111222 it_spfli-cityto.

else.

perform fill_cell using h 4 0 130230230 it_spfli-cityto.

endif.

if it_spfli-deptime lt '020000'.

perform fill_cell using h 5 0 145145145 it_spfli-deptime.

elseif it_spfli-deptime lt '120000' .

perform fill_cell using h 5 0 015215205 it_spfli-deptime.

elseif it_spfli-deptime lt '180000' .

perform fill_cell using h 5 0 000215205 it_spfli-deptime.

else.

perform fill_cell using h 5 0 115115105 it_spfli-deptime.

endif.

endloop.

  • EXCEL FILENAME

CONCATENATE SY-REPID '_' SY-DATUM6(2) '_' SY-DATUM4(2) '_'

SY-DATUM(4) '_' SY-UZEIT '.XLS' INTO FILENAME.

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

free object h_excel.

perform err_hdl.

----


  • FORM FILL_CELL *

----


  • sets cell at coordinates i,j to value val boldtype bold *

----


form fill_cell using i j bold col val.

call method of h_excel 'Cells' = h_zl

exporting

#1 = i

#2 = j.

perform err_hdl.

set property of h_zl 'Value' = val .

perform err_hdl.

get property of h_zl 'Font' = h_f.

perform err_hdl.

set property of h_f 'Bold' = bold .

perform err_hdl.

set property of h_f 'Color' = col.

perform err_hdl.

endform. "FILL_CELL

&----


*& Form ERR_HDL

&----


  • outputs OLE error if any *

----


  • --> p1 text

  • <-- p2 text

----


form err_hdl.

if sy-subrc <> 0.

write: / 'OLE-Automation Error:'(010), sy-subrc.

stop.

endif.

endform. " ERR_HDL

regards,

venkat.

4 REPLIES 4
Read only

Former Member
0 Likes
674

hi use this....

Report ZMULTICOLOR_TEST no standard page heading.

  • this report demonstrates how to send some ABAP data to an

  • EXCEL sheet using OLE automation.

include ole2incl.

  • handles for OLE objects

data: h_excel type ole2_object, " Excel object

h_mapl type ole2_object, " list of workbooks

h_map type ole2_object, " workbook

h_zl type ole2_object, " cell

h_f type ole2_object, " font

h_c type ole2_object. " color

DATA: FILENAME LIKE RLGRAP-FILENAME.

tables: spfli.

data h type i.

  • table of flights

data: it_spfli like spfli occurs 10 with header line.

&----


*& Event START-OF-SELECTION

&----


start-of-selection.

  • read flights

select * from spfli into table it_spfli.

  • display header

uline (61).

write: / sy-vline no-gap,

(3) 'Flg'(001) color col_heading no-gap, sy-vline no-gap,

(4) 'Nr'(002) color col_heading no-gap, sy-vline no-gap,

(20) 'Von'(003) color col_heading no-gap, sy-vline no-gap,

(20) 'Nach'(004) color col_heading no-gap, sy-vline no-gap,

(8) 'Zeit'(005) color col_heading no-gap, sy-vline no-gap.

uline /(61).

  • display flights

loop at it_spfli.

write: / sy-vline no-gap,

it_spfli-carrid color col_key no-gap, sy-vline no-gap,

it_spfli-connid color col_normal no-gap, sy-vline no-gap,

it_spfli-cityfrom color col_normal no-gap, sy-vline no-gap,

it_spfli-cityto color col_normal no-gap, sy-vline no-gap,

it_spfli-deptime color col_normal no-gap, sy-vline no-gap.

endloop.

uline /(61).

  • tell user what is going on

call function 'SAPGUI_PROGRESS_INDICATOR'

exporting

  • PERCENTAGE = 0

text = text-007

exceptions

others = 1.

  • start Excel

create object h_excel 'EXCEL.APPLICATION'.

  • PERFORM ERR_HDL.

set property of h_excel 'Visible' = 1.

  • CALL METHOD OF H_EXCEL 'FILESAVEAS' EXPORTING #1 = 'c:\kis_excel.xls' .

  • PERFORM ERR_HDL.

  • tell user what is going on

call function 'SAPGUI_PROGRESS_INDICATOR'

exporting

  • PERCENTAGE = 0

text = text-008

exceptions

others = 1.

  • get list of workbooks, initially empty

call method of h_excel 'Workbooks' = h_mapl.

perform err_hdl.

  • add a new workbook

call method of h_mapl 'Add' = h_map.

perform err_hdl.

  • tell user what is going on

call function 'SAPGUI_PROGRESS_INDICATOR'

exporting

  • PERCENTAGE = 0

text = text-009

exceptions

others = 1.

  • output column headings to active Excel sheet

perform fill_cell using 1 1 1 200 'Carrier id'(001).

perform fill_cell using 1 2 1 200 'Connection id'(002).

perform fill_cell using 1 3 1 200 'City from'(003).

perform fill_cell using 1 4 1 200 'City to'(004).

perform fill_cell using 1 5 1 200 'Dep. Time'(005).

loop at it_spfli.

  • copy flights to active EXCEL sheet

h = sy-tabix + 1.

if it_spfli-carrid cs 'AA'.

perform fill_cell using h 1 0 000255000 it_spfli-carrid.

elseif it_spfli-carrid cs 'AZ'.

perform fill_cell using h 1 0 168000000 it_spfli-carrid.

elseif it_spfli-carrid cs 'JL'.

perform fill_cell using h 1 0 168168000 it_spfli-carrid.

elseif it_spfli-carrid cs 'LH'.

perform fill_cell using h 1 0 111111111 it_spfli-carrid.

elseif it_spfli-carrid cs 'SQ'.

perform fill_cell using h 1 0 100100100 it_spfli-carrid.

else.

perform fill_cell using h 1 0 000145000 it_spfli-carrid.

endif.

if it_spfli-connid lt 400.

perform fill_cell using h 2 0 255000255 it_spfli-connid.

elseif it_spfli-connid lt 800.

perform fill_cell using h 2 0 077099088 it_spfli-connid.

else.

perform fill_cell using h 2 0 246156138 it_spfli-connid.

endif.

if it_spfli-cityfrom cp 'S*'.

perform fill_cell using h 3 0 155155155 it_spfli-cityfrom.

elseif it_spfli-cityfrom cp 'N*'.

perform fill_cell using h 3 0 189111222 it_spfli-cityfrom.

else.

perform fill_cell using h 3 0 111230222 it_spfli-cityfrom.

endif.

if it_spfli-cityto cp 'S*'.

perform fill_cell using h 4 0 200200200 it_spfli-cityto.

elseif it_spfli-cityto cp 'N*'.

perform fill_cell using h 4 0 000111222 it_spfli-cityto.

else.

perform fill_cell using h 4 0 130230230 it_spfli-cityto.

endif.

if it_spfli-deptime lt '020000'.

perform fill_cell using h 5 0 145145145 it_spfli-deptime.

elseif it_spfli-deptime lt '120000' .

perform fill_cell using h 5 0 015215205 it_spfli-deptime.

elseif it_spfli-deptime lt '180000' .

perform fill_cell using h 5 0 000215205 it_spfli-deptime.

else.

perform fill_cell using h 5 0 115115105 it_spfli-deptime.

endif.

endloop.

  • EXCEL FILENAME

CONCATENATE SY-REPID '_' SY-DATUM6(2) '_' SY-DATUM4(2) '_'

SY-DATUM(4) '_' SY-UZEIT '.XLS' INTO FILENAME.

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

free object h_excel.

perform err_hdl.

----


  • FORM FILL_CELL *

----


  • sets cell at coordinates i,j to value val boldtype bold *

----


form fill_cell using i j bold col val.

call method of h_excel 'Cells' = h_zl

exporting

#1 = i

#2 = j.

perform err_hdl.

set property of h_zl 'Value' = val .

perform err_hdl.

get property of h_zl 'Font' = h_f.

perform err_hdl.

set property of h_f 'Bold' = bold .

perform err_hdl.

set property of h_f 'Color' = col.

perform err_hdl.

endform. "FILL_CELL

&----


*& Form ERR_HDL

&----


  • outputs OLE error if any *

----


  • --> p1 text

  • <-- p2 text

----


form err_hdl.

if sy-subrc <> 0.

write: / 'OLE-Automation Error:'(010), sy-subrc.

stop.

endif.

endform. " ERR_HDL

regards,

venkat.

Read only

0 Likes
673

Hi Venkata,

Thanks for your answer. But it has not solved my issue.

I want an excel sheet embedded in my SAP screen.

Please let me know where I am wrong in my code.

Regards

Swastik

Read only

Former Member
0 Likes
673

Hi,

Use this code

REPORT ZP_EXCEL .

&----


*& Report ZP_EXCEL *

&----


include ole2incl.

data: application type ole2_object,

workbook type ole2_object,

sheet type ole2_object,

cells type ole2_object,

sheets TYPE ole2_object.

constants: row_max type i value 256.

data index type i.

data : det type ref to CL_ABAP_structDESCR,

wa like line of det->components.

data: begin of i_finaltab occurs 0,

Col1 type i,

col2 type i,

col3 type i,

col4 type i,

end of i_finaltab.

data : count type i,

v_excel_count(3), " type i value 1,

sheet_name(15) .

************************************************************************

*START-OF-SELECTION

start-of-selection.

do 100 times.

count = count + 1.

i_finaltab-col2 = 5 * count.

i_finaltab-col3 = 10 * count.

i_finaltab-col4 = 20 * count.

move : count to i_finaltab-col1.

append i_finaltab.

clear i_finaltab.

enddo.

clear count.

create object application 'excel.application'.

set property of application 'visible' = 1.

call method of application 'Workbooks' = workbook.

call method of workbook 'Add'.

      • Create first Excel Sheet

call method of application 'Worksheets' = sheet

exporting

#1 = 1.

call method of sheet 'Activate'.

set property of sheet 'Name' = 'EXCEL0'.

count = 1.

index = row_max * ( count - 1 ) + 1.

perform header_details.

DATA FNAME(60) VALUE '/data/sapdata/mk112.xls'.

OPEN DATASET FNAME FOR OUTPUT.

loop at i_finaltab.

count = count + 1.

if count LE 6.

index = row_max * ( count - 1 ) + 1. " 1 - column name

call method of sheet 'Cells' = cells

exporting

#1 = index.

set property of cells 'Value' = i_finaltab-col1.

index = index + 1.

call method of sheet 'Cells' = cells

exporting

#1 = index.

set property of cells 'Value' = i_finaltab-col2.

index = index + 1.

call method of sheet 'Cells' = cells

exporting

#1 = index.

set property of cells 'Value' = i_finaltab-col3.

index = index + 1.

call method of sheet 'Cells' = cells

exporting

#1 = index.

set property of cells 'Value' = i_finaltab-col4.

else.

v_excel_count = v_excel_count + 1.

CALL METHOD OF application 'Sheets' = sheets.

CALL METHOD OF sheets 'Add'.

concatenate 'EXCEL' v_excel_count into sheet_name.

clear count.

count = count + 1.

call method of application 'Worksheets' = sheet

exporting

#1 = 1.

set property of sheet 'Name' = sheet_name.

call method of sheet 'Activate'.

index = row_max * ( count - 1 ) + 1.

perform header_details.

count = count + 1.

index = row_max * ( count - 1 ) + 1. " 1 - column name

call method of sheet 'Cells' = cells

exporting

#1 = index.

set property of cells 'Value' = i_finaltab-col1.

index = index + 1.

call method of sheet 'Cells' = cells

exporting

#1 = index.

set property of cells 'Value' = i_finaltab-col2.

index = index + 1.

call method of sheet 'Cells' = cells

exporting

#1 = index.

set property of cells 'Value' = i_finaltab-col3.

index = index + 1.

call method of sheet 'Cells' = cells

exporting

#1 = index.

set property of cells 'Value' = i_finaltab-col4.

endif.

endloop.

&----


*& Form header_details

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form header_details.

det ?= cl_abap_typedescr=>describe_by_DATA( i_finaltab ).

loop at det->components into wa.

call method of sheet 'Cells' = cells

exporting

#1 = index.

set property of cells 'Value' = wa-name.

index = index + 1.

endloop.

endform. " header_details

Read only

0 Likes
673

Hi Murali,

Thanks for your answer. But it has not solved my issue.

I want an excel sheet embedded in my SAP screen.

Please let me know where I am wrong in my code.

Regards

Swastik