Hi All,
We are going to learn how to do various EXCEL operations using OLE Concept.
Using OLE concept provided by SAP for ABAP, we can do many EXCEL operations in the background(backend) like Selecting rows, cutting cells, pasting data into cells, controlling the font used in individual cells and so on which we normally do in the front end.
I have used this OLE concept to accomplish the following-
Lets say, this is the look and feel of the Selection Screen.
- In the selection screen, there is a select option 'Table Name' where the user can give any number of Z table names.
- The Button Download EXCEL Template downloads the EXCEL template with a list of worksheets, one worksheet for each of the Z tables specified in the Selection Screen.
1. Note that There are 2 wprksheets one for each Z-table specified in the Selection Screen.
This is achieved using OLE concept of ABAP as below -
CREATE OBJECT lo_application 'Excel.Application'.
CALLMETHODOF
lo_application
'Workbooks' = lo_workbook.
DESCRIBETABLE s_table[] LINES w_tables_cnt.
CALLMETHODOF
lo_workbook
'Add'.
SETPROPERTYOF lo_application 'Visible' = 0.
CALLMETHODOF
lo_application
'Worksheets' = lo_worksheet.
LOOPAT s_table.
w_sheet_no = sy-tabix.
CREATEDATA wa_dref TYPETABLEOF (s_table-low).
ASSIGN wa_dref->* TO<t_itab>.
desc_table ?= cl_abap_tabledescr=>describe_by_data( <t_itab> ).
desc_struc ?= desc_table->get_table_line_type( ).
IF w_sheet_no = 1.
CALLMETHODOF
lo_worksheet
'Add'.
CALLMETHODOF
lo_application
'Worksheets' = lo_worksheet
EXPORTING
#1 = w_sheet_no.
CALLMETHODOF
lo_worksheet
'Activate'.
SETPROPERTYOF lo_worksheet 'Name' = s_table-low.
ELSE.
GETPROPERTYOF lo_application 'Sheets' = lo_worksheet .
CALLMETHODOF
lo_worksheet
'Add' = lo_workbook.
SETPROPERTYOF lo_workbook 'Name' = s_table-low .
GETPROPERTYOF lo_application 'ACTIVESHEET' = lo_worksheet.
ENDIF.
desc_struc1 ?= cl_abap_structdescr=>describe_by_name( s_table-low ).
t_ddic_info = desc_struc1->get_ddic_field_list( ).
CLEAR w_column.
REFRESH lt_data[].
LOOPAT desc_struc->components ASSIGNING<fs_component>.
w_column = w_column + 1.
CLEAR wa_ddic_info.
READTABLE t_ddic_info INTO wa_ddic_info WITHKEY fieldname = <fs_component>-name.
IF wa_ddic_info-keyflag = 'X'.
PERFORM f300_fill_cell USING1 w_column 1<fs_component>-name.
ELSE.
PERFORM f300_fill_cell USING1 w_column 0<fs_component>-name.
ENDIF.
ASSIGN<fs_component>-name TO<field>.
CONCATENATE lt_data <field> INTO lt_data SEPARATEDBY cl_abap_char_utilities=>horizontal_tab.
SHIFT lt_data BY1PLACESLEFT.
APPEND lt_data.
CLEAR lt_data.
ENDLOOP.
CALLFUNCTION'CONTROL_FLUSH'
EXCEPTIONS
OTHERS = 3.
CALLFUNCTION'CLPB_EXPORT'
TABLES
data_tab = lt_data
EXCEPTIONS
clpb_error = 1
OTHERS = 2.
ENDLOOP.
IF w_sheet_no > 1.
CALLMETHODOF
lo_workbook
'SaveAs'
EXPORTING
#1 = l_fullpath.
IF sy-subrc EQ0.
MESSAGE'File downloaded successfully'TYPE'S'.
ELSE.
MESSAGE'Error downloading the file'TYPE'E'.
ENDIF.
ELSE.
CALLMETHODOF
lo_worksheet
'SaveAs'
EXPORTING
#1 = l_fullpath.
IF sy-subrc EQ0.
MESSAGE'File downloaded successfully'TYPE'S'.
ELSE.
MESSAGE'Error downloading the file'TYPE'E'.
ENDIF.
ENDIF.
CALLMETHODOF
lo_application
'QUIT'.
2. Note that the Key fields in the EXCEL are made bold.
This is accomplished by using OLE concept as below -
CALLMETHODOF
lo_worksheet
'Cells' = lo_cell
NO
FLUSH
EXPORTING
#1 = i
#2 = j.
SETPROPERTYOF lo_cell 'VALUE' = val NO FLUSH.
GETPROPERTYOF lo_cell 'FONT' = lo_font NO FLUSH.
SETPROPERTYOF lo_font 'BOLD' = bold NO FLUSH.
- The Button 'Upload EXCEL Data' reads data from all the WorkSheets and uploads data into the corresponding tables.
This concept of reading data from multiple sheets from the EXCEL is accomplished using OLE concept as below -
CREATE OBJECT lo_application 'Excel.Application'.
CALLMETHODOF
lo_application
'Workbooks' = lo_workbook.
READTABLE t_filetable INTO wa_filetable INDEX1.
w_filename = wa_filetable-filename.
CALLMETHODOF
lo_workbook
'Open'
EXPORTING
#1 = w_filename.
DESCRIBETABLE s_table LINES w_tabcnt_sel.
DO w_tabcnt_sel TIMES.
w_sheet_no = w_sheet_no + 1.
GETPROPERTYOF lo_application 'ACTIVESHEET' = lo_worksheet.
CALLMETHODOF
lo_application
'Worksheets' = lo_worksheet
EXPORTING
#1 = w_sheet_no.
CALLMETHODOF
lo_worksheet
'Activate'.
GETPROPERTYOF lo_application 'ACTIVESHEET' = lo_worksheet.
GETPROPERTYOF lo_worksheet 'Name' = w_sheet_name.
IF w_sheet_name ISNOTINITIALAND w_sheet_name IN s_table[].
ELSE.
w_subrc = 4.
CONTINUE.
ENDIF.
CREATEDATA wa_dref TYPETABLEOF (w_sheet_name).
ASSIGN wa_dref->* TO<t_itab>.
CREATEDATA wa_dref LIKELINEOF<t_itab>.
ASSIGN wa_dref->* TO<wa_itab>.
desc_table ?= cl_abap_tabledescr=>describe_by_data( <t_itab> ).
desc_struc ?= desc_table->get_table_line_type( ).
DESCRIBETABLE desc_struc->components LINES w_column_cnt.
w_row_cnt = p_record.
* mark whole spread sheet
CALLMETHODOF
lo_worksheet
'Cells' = h_cell
EXPORTING
#1 = 2
#2 = 1.
CALLMETHODOF
lo_worksheet
'Cells' = h_cell1
EXPORTING
#1 = w_row_cnt
#2 = w_column_cnt.
CALLMETHODOF
lo_worksheet
'RANGE' = range
EXPORTING
#1 = h_cell
#2 = h_cell1.
CALLMETHODOF
range
'SELECT'.
CALLMETHODOF
range
'COPY'.
CALLMETHOD cl_gui_frontend_services=>clipboard_import
IMPORTING
data = t_excel_tab
EXCEPTIONS
cntl_error = 1
OTHERS = 4.
LOOPAT t_excel_tab INTO wa_excel_tab.
SPLIT wa_excel_tab-lineAT w_tab INTOTABLE t_split_data.
CLEAR w_count.
LOOPAT t_split_data INTO wa_split_data.
ADD1TO w_count.
ASSIGNCOMPONENT w_count OFSTRUCTURE<wa_itab> TO<field>.
<field> = wa_split_data-line.
ENDLOOP.
APPEND<wa_itab> TO<t_itab>.
ENDLOOP.
IF<t_itab> ISNOTINITIAL.
MODIFY (w_sheet_name) FROMTABLE<t_itab>.
IF sy-subrc = 0.
wa_tabsuccess-tabname = w_sheet_name.
APPEND wa_tabsuccess TO t_tabsuccess.
ELSE.
wa_tabfailure-tabname = w_sheet_name.
APPEND wa_tabfailure TO t_tabfailure.
ENDIF.
ELSE.
wa_tabfailure-tabname = w_sheet_name.
APPEND wa_tabfailure TO t_tabfailure.
ENDIF.
ENDDO.
SUMMARY: OLE Concept provided by SAP is a good approach to perform various EXCEL operations in background.