03-19-2009 10:13 AM
Hi All,
I have a requirement where the Report Output should be displayed directly in Excel.
I am using OLE objects for this.
Could anyone provide me with all the Properties of Excel which can be used in the coding.
For example, Setting Row Height, formatting the row and setting a pattern for it.
Setting Borders for cells/columns/rows etc.
Any suggestions/ideas provided would be of great help.
Thanks in advance.
Regards,
Adapala M.
03-19-2009 10:22 AM
Hi,
Use FM
FTBU_START_EXCEL
MS_EXCEL_OLE_STANDARD_DAT
Use:
DATA: w_file TYPE rlgrap-filename VALUE 'it_value'.
CALL FUNCTION 'FTBU_START_EXCEL'
EXPORTING
data_name = w_file
data_path_flag = 'T'
*---Start Excel and not Wait
wait = ' '
TABLES
data_tab = <itab>
EXCEPTIONS
no_batch = 1
excel_not_installed = 2
wrong_version = 3
internal_error = 4
invalid_type = 5
cancelled = 6
download_error = 7
OTHERS = 8.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
Hope this helps you.
Regards,
Tarun
03-19-2009 10:22 AM
Hi,
Use FM
FTBU_START_EXCEL
MS_EXCEL_OLE_STANDARD_DAT
Use:
DATA: w_file TYPE rlgrap-filename VALUE 'it_value'.
CALL FUNCTION 'FTBU_START_EXCEL'
EXPORTING
data_name = w_file
data_path_flag = 'T'
*---Start Excel and not Wait
wait = ' '
TABLES
data_tab = <itab>
EXCEPTIONS
no_batch = 1
excel_not_installed = 2
wrong_version = 3
internal_error = 4
invalid_type = 5
cancelled = 6
download_error = 7
OTHERS = 8.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
Hope this helps you.
Regards,
Tarun
03-19-2009 10:31 AM
Hi,
The following SAP tables are associated with OLE
● TOLE OLE Applications
● OLELOAD OLE type Information load
● SWOTOLE Workflow Object Types OLE Applications
● SWOTTOLE Workflow Object Types Texts OLE Applications
● TOLET Workflow Object Types Texts OLE Applications
The following ABAP key words control the applications:
● CREATE OBJECT
● SET PROPERTY
● GET PROPERTY
● CALL METHOD
● FREE OBJECT
Hope this helps you.
Regards,
Manoj Kumar P
Edited by: Manoj Kumar on Mar 19, 2009 11:32 AM
03-19-2009 10:45 AM
Hi,
The following properties can be used using OLE for excel download:
Create EXCEL.
CREATE OBJECT ob_l_excel 'EXCEL.APPLICATION'.
IF sy-subrc NE 0.
WRITE: / 'No EXCEL creation possible'.
STOP.
ENDIF.
SET PROPERTY OF ob_l_excel 'DisplayAlerts' = 0.
Create Workbook
CALL METHOD OF ob_l_excel 'WORKBOOKS' = ob_l_workbook .
SET PROPERTY OF ob_l_excel 'VISIBLE' = 1.
Create Worksheet
SET PROPERTY OF ob_l_excel 'SheetsInNewWorkbook' = 1.
CALL METHOD OF ob_l_workbook 'ADD'.
to set properties at the time of display.
CALL METHOD OF ob_l_excel 'cells' = ob_l_cell
EXPORTING
#1 = row
#2 = col.
SET PROPERTY OF ob_l_cell 'value' = value.
GET PROPERTY OF ob_l_cell 'font' = ob_l_font.
SET PROPERTY OF ob_l_font 'bold' = bold.
CALL METHOD OF ob_l_excel 'columns' = ob_l_column.
CALL METHOD OF ob_l_column 'autofit'.
03-19-2009 10:46 AM
Hi Adap,
You can refer t-code DWDM, this is a set of demos and you can find excel OLE related beautifully explained in this.
SR
03-19-2009 10:49 AM
Hi,
Kindly go through this sample code below:
INCLUDE ole2incl. "include used for providing classes used for using create object for creating application and worksheets
DATA: application TYPE ole2_object,
workbook TYPE ole2_object,
sheet TYPE ole2_object,
cells TYPE ole2_object.
CONSTANTS: row_max TYPE i VALUE 256.
DATA index TYPE i.
*DATA: BEGIN OF itab1 OCCURS 0, first_name(10), END OF itab1.
*DATA: BEGIN OF itab2 OCCURS 0, last_name(10), END OF itab2.
DATA: BEGIN OF itab3 OCCURS 0, formula(50), END OF itab3.
*
TABLES: vbap,mara.
TYPES: BEGIN OF itab,
lifnr TYPE lfa1-lifnr,
land1 TYPE lfa1-land1,
* name1 TYPE lfa1-name1,
* ort01 TYPE lfa1-ort01,
END OF itab.
DATA: BEGIN OF itab2 OCCURS 0,
matnr TYPE mara-matnr,
ersda TYPE mara-ersda,
ernam TYPE mara-ernam,
END OF itab2.
data : v_row type sy-tabix.
DATA: itab1 TYPE STANDARD TABLE OF itab WITH HEADER LINE.
DATA: IT_XLSTAB TYPE STANDARD TABLE OF ITAB ,
WA_XLSTAB LIKE LINE OF IT_XLSTAB.
START-OF-SELECTION.
SELECT lifnr land1 fROM lfa1 INTO CORRESPONDING FIELDS OF TABLE itab1 UP TO 5 ROWS.
SELECT matnr
ersda
ernam
FROM mara
INTO CORRESPONDING FIELDS OF
TABLE itab2 UP TO 5 ROWS.
************************************************************************
*START-OF-SELECTION
START-OF-SELECTION.
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' = 'Sheet1'.
clear v_row.
LOOP AT itab1.
v_row = sy-tabix.
perform fill_cell using v_row 1 itab1-lifnr.
perform fill_cell using v_row 2 itab1-land1.
ENDLOOP.
* Create second Excel sheet
CALL METHOD OF application 'Worksheets' = sheet
EXPORTING #1 = 2.
SET PROPERTY OF sheet 'Name' = 'Sheet2'.
CALL METHOD OF sheet 'Activate'.
clear v_row.
LOOP AT itab2.
v_row = sy-tabix.
perform fill_cell using v_row 1 itab2-matnr.
perform fill_cell using v_row 2 itab2-ersda.
perform fill_cell using v_row 3 itab2-ernam.
ENDLOOP.
* Save excel speadsheet to particular filename
CALL METHOD OF sheet 'SaveAs'
EXPORTING #1 = 'c:\temp\excelgeet.xls' "filename
#2 = 1. "fileFormat
* Closes excel window, data is lost if not saved
SET PROPERTY OF application 'visible' = 0.
* call method of sheet 'CLOSE'
*
* EXPORTING #1 = 'YES'.
*&---------------------------------------------------------------------*
*& both the below coding closes the apllication permanently from the task manager also.
*&---------------------------------------------------------------------*
SET PROPERTY OF application 'DisplayAlerts' = 0.
free OBJECT application.
** call method of application 'QUIT'.
**
** FREE OBJECT: APPLICATION,
** SHEET.
*&---------------------------------------------------------------------*
*& Form fill_cell
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_1 text
* -->P_V_COL text
* -->P_ITAB1_LIFNR text
*----------------------------------------------------------------------*
form fill_cell using row col val.
CALL METHOD OF sheet 'Cells' = cells EXPORTING #1 = row #2 = col.
SET PROPERTY OF cells 'Value' = val.
endform. " fill_cell
This is giving the two internal table data output in one excel files 2 sheets.
Hope it helps
Regards
Mansi
03-19-2009 10:55 AM
Hi,
You can take help from this code.
Create OLE ojbect for Excel app
CREATE OBJECT gh_excel co_xl_app.
PERFORM err_handle.
Set invisalbe for the app
SET PROPERTY OF gh_excel co_xl_visible = 0.
PERFORM err_handle.
get list of workbooks, initially empty
CALL METHOD OF gh_excel co_xl_book = gh_mapl.
PERFORM err_handle.
add a new workbook
CALL METHOD OF gh_mapl co_xl_add = gh_map.
PERFORM err_handle.
Write Dealer Name label
PERFORM fill_cell
USING lv_row_idx lv_col_idx 1 text-t04.
*Set grid lines for the downloaded excel file
*the range start from A1 cell and end with N column and the total rows
CLEAR lv_str_temp.
lv_str_temp = gv_tot_rows.
CONDENSE lv_str_temp.
lv_range_begin = 'A3'.
lv_range_end = 'M'.
CONCATENATE lv_range_end lv_str_temp INTO lv_range_end.
CONDENSE lv_range_end.
CALL METHOD OF gh_excel co_xl_range = gh_range
NO FLUSH
EXPORTING #1 = lv_range_begin
#2 = lv_range_end.
PERFORM err_handle.
left edge border
CALL METHOD OF gh_range co_xl_borders = gh_borders
NO FLUSH
EXPORTING #1 = co_xl_edge_left.
PERFORM err_handle.
SET PROPERTY OF gh_borders co_xl_linestyle = co_xl_continuous.
right edge border
CALL METHOD OF gh_range co_xl_borders = gh_borders
NO FLUSH
EXPORTING #1 = co_xl_edge_right.
PERFORM err_handle.
SET PROPERTY OF gh_borders co_xl_linestyle = co_xl_continuous.
top edge border
CALL METHOD OF gh_range co_xl_borders = gh_borders
NO FLUSH
EXPORTING #1 = co_xl_edge_top.
PERFORM err_handle.
SET PROPERTY OF gh_borders co_xl_linestyle = co_xl_continuous.
bottom edge border
CALL METHOD OF gh_range co_xl_borders = gh_borders
NO FLUSH
EXPORTING #1 = co_xl_edge_bottom.
PERFORM err_handle.
SET PROPERTY OF gh_borders co_xl_linestyle = co_xl_continuous.
inside vertical border
CALL METHOD OF gh_range co_xl_borders = gh_borders
NO FLUSH
EXPORTING #1 = co_xl_edge_vinside.
PERFORM err_handle.
SET PROPERTY OF gh_borders co_xl_linestyle = co_xl_continuous.
inside horizonal border
CALL METHOD OF gh_range co_xl_borders = gh_borders
NO FLUSH
EXPORTING #1 = co_xl_edge_hinside.
PERFORM err_handle.
SET PROPERTY OF gh_borders co_xl_linestyle = co_xl_continuous.
*Set auto fit width for the columns
DO co_excel_col_no TIMES.
PERFORM set_auto_fit USING sy-index.
ENDDO.
&----
*& Form set_auto_fit
&----
text
----
-->P_SY_INDEX text
----
FORM set_auto_fit USING p_col_number TYPE i.
CALL METHOD OF gh_excel co_xl_cols = gh_cols
NO FLUSH
EXPORTING #1 = p_col_number.
PERFORM err_handle.
CALL METHOD OF gh_cols co_xl_entcol = gh_entcol NO FLUSH .
PERFORM err_handle.
CALL METHOD OF gh_entcol co_xl_autofit NO FLUSH.
PERFORM err_handle.
ENDFORM.
&----
*& Form fill_cell
&----
text
----
-->P_1 text
-->P_1 text
-->P_1 text
-->P_TEXT_019 text
----
FORM fill_cell USING i j bold val.
CALL METHOD OF gh_excel co_xl_cells = gh_zl
NO FLUSH
EXPORTING #1 = i #2 = j.
PERFORM err_handle.
SET PROPERTY OF gh_zl co_xl_value = val no flush.
PERFORM err_handle.
GET PROPERTY OF gh_zl co_xl_font = gh_f no flush.
PERFORM err_handle.
SET PROPERTY OF gh_f co_xl_bold = bold no flush.
PERFORM err_handle.
ENDFORM. " fill_cell
Hope it will help you.
regards,
Lokesh
03-20-2009 11:20 AM
Hi Lokesh,
I have a problem with LineStyle-property.
When I change it via ABAP, I get this error-message (my translation into english):
(Error)(20.03.09 11:40:09.956): SET PROPERTY "LineStyle"[DispID=119] OF [#60/0x0BE93194/1402/Excel.Application]
#0: STRING "xlContinuous"
IDispatch::Invoke raised exception
(Error) :
(Error) : ****************************ERROR OCCURED IN MODULE: [Microsoft Office Excel-Application]*********************************************************************************************************************************************************
(Error) : PROGRAM_ID |MODULE_NAME |METHOD_NAME |ERROR DESCRIPTION |VERSION |GUI VERSION |MODULE_PATH |
(Error) : **************************************************************************************************************************************************************************************************************************************************
(Error) : Excel.Application |Microsoft Office Excel-Application |LineStyle | LineStyle-Property of the Border-Object may not be changed. |11.0.6560.0 |7100.2.7.1038 |C:\PROGRA1\MICROS2\OFFICE11\EXCEL.EXE |
(Error) : **************************************************************************************************************************************************************************************************************************************************
(Error) :
(Error) : Exception fire by :Microsoft Office Excel
(Error) : Exception info:LineStyle-property of border-object my not be changed.
(Error) : Exception code:0
When I look in the objectbrowser of Excels VB-Editor, I see, that Range.Borders is a readonly property.
So how you manage to get this property changed.
I am using R/3 4.6c and MS Excel 2003 + 2007.
Regards,
Hubert
03-23-2009 10:08 AM
03-23-2009 11:58 AM
REPORT ZOLE_ABAP.
INCLUDE ole2incl.
*&----------------------------------------------------*
*& TYPES *
*&----------------------------------------------------*
TYPES: BEGIN OF ty_spfli,
kunnr TYPE kna1-kunnr,
land1 TYPE kna1-land1,
NAME1 TYPE KNA1-NAME1,
ORT01 TYPE KNA1-ORT01,
REGIO TYPE KNA1-REGIO,
ADRNR TYPE KNA1-ADRNR,
END OF ty_spfli.
TYPES: BEGIN OF ty_titles,
title(20) TYPE c,
field(20) TYPE c,
END OF ty_titles.
*&----------------------------------------------------*
*& INTERNAL TABLES *
*&----------------------------------------------------*
DATA: t_spfli TYPE STANDARD TABLE OF ty_spfli,
t_titles TYPE STANDARD TABLE OF ty_titles.
*&----------------------------------------------------*
*& FIELD-SYMBOLS *
*&----------------------------------------------------*
FIELD-SYMBOLS: <fs_spfli> LIKE LINE OF t_spfli,
<fs_titles> LIKE LINE OF t_titles,
<fs> TYPE ANY.
*&----------------------------------------------------*
*& VARIABLES *
*&----------------------------------------------------*
DATA: w_tabix TYPE sy-tabix,
w_titles TYPE sy-tabix,
w_line TYPE sy-tabix,
w_field TYPE string,
filename TYPE string,
path TYPE string,
fullpath TYPE string.
DATA: data_titles TYPE REF TO data.
DATA: e_sheet TYPE ole2_object,
e_activesheet TYPE ole2_object,
e_newsheet TYPE ole2_object,
e_appl TYPE ole2_object,
e_work TYPE ole2_object,
e_cell TYPE ole2_object,
e_color TYPE ole2_object,
e_bold TYPE ole2_object.
*&----------------------------------------------------*
*& SELECTION-SCREEN *
*&----------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1.
PARAMETERS: p_file TYPE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK b1.
*&----------------------------------------------------*
*& START-OF-SELECTION *
*&----------------------------------------------------*
START-OF-SELECTION.
PERFORM get_titles.
PERFORM get_data.
PERFORM create_excel.
*&----------------------------------------------------*
*& AT SELECTION-SCREEN *
*&----------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = 'Select archivo'
default_extension = 'xls'
file_filter = '*.xls'
CHANGING
filename = filename
path = path
fullpath = fullpath.
IF sy-subrc EQ 0.
p_file = fullpath.
ENDIF.
*&----------------------------------------------------*
*& Form get_titles *
*&----------------------------------------------------*
FORM get_titles.
CREATE DATA data_titles TYPE ty_titles.
ASSIGN data_titles->* TO <fs_titles>.
<fs_titles>-title = 'Customer Number 1'.
<fs_titles>-field = 'KUNNR'.
APPEND <fs_titles> TO t_titles.
<fs_titles>-title = 'Country Key'.
<fs_titles>-field = 'LAND1'.
APPEND <fs_titles> TO t_titles.
<fs_titles>-title = 'Name 1'.
<fs_titles>-field = 'NAME1'.
APPEND <fs_titles> TO t_titles.
<fs_titles>-title = 'City'.
<fs_titles>-field = 'ORT01'.
APPEND <fs_titles> TO t_titles.
<fs_titles>-title = 'Region'.
<fs_titles>-field = 'REGIO'.
APPEND <fs_titles> TO t_titles.
<fs_titles>-title = 'Address'.
<fs_titles>-field = 'ADRNR'.
APPEND <fs_titles> TO t_titles.
ENDFORM. "get_titles
*&----------------------------------------------------*
*& Form get_data *
*&----------------------------------------------------*
FORM get_data.
SELECT KUNNR LAND1 NAME1 ORT01 REGIO ADRNR
INTO TABLE t_spfli
FROM KNA1
WHERE LAND1 EQ 'IN'
and KUNNR EQ '0000700008'.
ENDFORM. " get_data
*&----------------------------------------------------*
*& Form create_excel *
*&----------------------------------------------------*
FORM create_excel.
w_line = 1.
CREATE OBJECT e_appl 'EXCEL.APPLICATION'.
SET PROPERTY OF e_appl 'VISIBLE' = 1.
CALL METHOD OF e_appl 'WORKBOOKS' = e_work.
CALL METHOD OF e_work 'Add' = e_work.
GET PROPERTY OF e_appl 'ActiveSheet' = e_activesheet.
SET PROPERTY OF e_activesheet 'Name' = 'Customer Details'.
LOOP AT t_spfli ASSIGNING <fs_spfli>.
w_tabix = sy-tabix.
w_line = w_line + 1.
LOOP AT t_titles ASSIGNING <fs_titles>.
w_titles = sy-tabix.
CALL METHOD OF e_appl 'Cells' = e_cell
EXPORTING
#1 = 1
#2 = w_titles.
SET PROPERTY OF e_cell 'Value' = <fs_titles>-title.
GET PROPERTY OF e_cell 'Interior' = e_color.
SET PROPERTY OF e_color 'ColorIndex' = 35.
GET PROPERTY OF e_cell 'Font' = e_bold.
SET PROPERTY OF e_bold 'Bold' = 1.
CALL METHOD OF e_appl 'Cells' = e_cell
EXPORTING
#1 = w_line
#2 = w_titles.
CONCATENATE '<fs_spfli>-' <fs_titles>-field
INTO w_field.
ASSIGN (w_field) TO <fs>.
SET PROPERTY OF e_cell 'Value' = <fs>.
GET PROPERTY OF e_cell 'Interior' = e_color.
SET PROPERTY OF e_cell 'ColumnWidth' = 20.
SET PROPERTY OF e_color 'ColorIndex' = 0.
GET PROPERTY OF e_cell 'Font' = e_bold.
SET PROPERTY OF e_bold 'Bold' = 0.
ENDLOOP.
ENDLOOP.
CALL METHOD OF e_work 'SAVEAS'
EXPORTING
#1 = p_file.
CALL METHOD OF e_work 'close'.
CALL METHOD OF e_appl 'QUIT'.
FREE OBJECT e_appl.
ENDFORM. " create_excel
03-30-2009 1:00 PM
Hi All,
Thanks all of you for your valuable inputs.
I have one issue now.
I need to freeze the panes in the Excel sheet. Could anyone help me how to do it?
Regards,
Adapala M.