REPORT ZTEST_EXC.
INCLUDE ole2incl.
*DATA:BEGIN OF excel_tab OCCURS 0,
* TEXT(10) TYPE C,
* END OF EXCEL_TAB.
*
*DATA:BEGIN OF excel_tab1 OCCURS 0,
* TEXT(10) TYPE C,
* END OF EXCEL_TAB1.
DATA: EXCEL_TAB LIKE TABLE OF ZEXCEL WITH HEADER LINE.
DATA: EXCEL_TAB1 LIKE TABLE OF ZEXCEL WITH HEADER LINE.
DATA: ld_separator TYPE c.
DATA: application TYPE ole2_object,
workbook TYPE ole2_object,
SHEET TYPE OLE2_OBJECT,
range TYPE ole2_object,
worksheet TYPE ole2_object.
DATA: h_cell TYPE ole2_object,
h_cell1 TYPE ole2_object.
DATA: ld_rc TYPE i.
* MESSAGE DEFINATION
DEFINE m_message.
case sy-subrc.
when 0.
when 1.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
when others. raise upload_ole.
endcase.
END-OF-DEFINITION.
SELECTION-SCREEN BEGIN OF BLOCK I WITH FRAME.
PARAMETERS: FILE LIKE RLGRAP-FILENAME,
S_ROW_S1 TYPE I,
S_C_S1 TYPE I,
S_ROW_S2 TYPE I,
S_C_S2 TYPE I,
E_ROW_S1 TYPE I,
E_C_S1 TYPE I,
E_ROW_S2 TYPE I,
E_C_S2 TYPE I.
SELECTION-SCREEN END OF BLOCK I.
* PARAMETER CHECK
IF S_ROW_S1 > E_ROW_S1.
RAISE inconsistent_parameters.
ENDIF.
IF S_C_S1 > E_C_S1.
RAISE inconsistent_parameters.
ENDIF.
IF S_ROW_S2 > E_ROW_S2.
RAISE inconsistent_parameters.
ENDIF.
IF S_C_S2 > E_C_S2.
RAISE inconsistent_parameters.
ENDIF.
CLASS cl_abap_char_utilities DEFINITION LOAD.
ld_separator = cl_abap_char_utilities=>horizontal_tab.
* OPENING EXCEL FILE
IF application-header = space OR application-handle = -1.
CREATE OBJECT application 'Excel.Application'.
m_message.
ENDIF.
CALL METHOD OF APPLICATION 'Workbooks' = WORKBOOK.
m_message.
CALL METHOD OF application 'Workbooks' = workbook.
m_message.
CALL METHOD OF workbook 'Open' EXPORTING #1 = FILE.
m_message.
CALL METHOD OF APPLICATION 'Worksheets' = SHEET EXPORTING #1 = 1.
m_message.
CALL METHOD OF APPLICATION 'Worksheets' = SHEET EXPORTING #1 = 1.
m_message.
CALL METHOD OF SHEET 'Activate'.
m_message.
GET PROPERTY OF application 'ACTIVESHEET' = sheet.
m_message.
* MARKING OF WHOLE SPREADSHEET
CALL METHOD OF sheet 'Cells' = h_cell
EXPORTING #1 = S_ROW_S1 #2 = S_C_S1.
m_message.
CALL METHOD OF sheet 'Cells' = h_cell1
EXPORTING #1 = E_ROW_S1 #2 = E_C_S1.
m_message.
CALL METHOD OF sheet 'RANGE' = range
EXPORTING #1 = h_cell #2 = h_cell1.
m_message.
CALL METHOD OF range 'SELECT'.
m_message.
* Copy marked area (SHEET1) into Clippboard
CALL METHOD OF range 'COPY'.
m_message.
* Read clipboard into ABAP
CALL METHOD cl_gui_frontend_services=>clipboard_import
IMPORTING
data = excel_tab[]
EXCEPTIONS
cntl_error = 1
* ERROR_NO_GUI = 2
* NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4
.
IF sy-subrc <> 0.
MESSAGE a037(alsmex).
ENDIF.
*PERFORM separated_to_intern_convert TABLES excel_tab IT_DATA1
*USING ld_separator.
* Clear the clipboard
REFRESH excel_tab.
CALL METHOD cl_gui_frontend_services=>clipboard_export
IMPORTING
data = excel_tab[]
CHANGING
rc = ld_rc
EXCEPTIONS
cntl_error = 1
* ERROR_NO_GUI = 2
* NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4
.
* Working in Second Excel Work Sheet
CALL METHOD OF APPLICATION 'Worksheets' = SHEET EXPORTING #1 = 2.
m_message.
CALL METHOD OF SHEET 'Activate'.
m_message.
GET PROPERTY OF application 'ACTIVESHEET' = sheet.
m_message.
* Mark Sheet2
CALL METHOD OF sheet 'Cells' = h_cell
EXPORTING #1 = S_ROW_S2 #2 = S_C_S2.
m_message.
CALL METHOD OF sheet 'Cells' = h_cell1
EXPORTING #1 = E_ROW_S2 #2 = E_C_S2.
m_message.
CALL METHOD OF sheet 'RANGE' = range
EXPORTING #1 = h_cell #2 = h_cell1.
m_message.
CALL METHOD OF range 'SELECT'.
m_message.
* Copy Marked Area (Sheet2) into Clippboard
CALL METHOD OF range 'COPY'.
m_message.
* Read Clipboard into ABAP
CALL METHOD cl_gui_frontend_services=>clipboard_import
IMPORTING
data = excel_tab1[]
EXCEPTIONS
cntl_error = 1
* ERROR_NO_GUI = 2
* NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4
.
IF sy-subrc <> 0.
MESSAGE a037(alsmex).
ENDIF.
*PERFORM separated_to_intern_convert TABLES excel_tab1 IT_DATA2
*USING ld_separator.
* Clear Clipboard
REFRESH excel_tab.
CALL METHOD cl_gui_frontend_services=>clipboard_export
IMPORTING
data = excel_tab1[]
CHANGING
rc = ld_rc
EXCEPTIONS
cntl_error = 1
* ERROR_NO_GUI = 2
* NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.
* Leaving Application .
CALL METHOD OF application 'QUIT'.
m_message.
FREE OBJECT application.
m_message.