0% found this document useful (0 votes)
216 views

ZTEST Excel

The document defines classes and modules to create objects for interacting with an Excel spreadsheet using SAP Desktop Office Integration interfaces. Key objects created include a document proxy, spreadsheet interface, and event handler. The modules upload a file, open it in Excel, select sheets and ranges, and insert data.

Uploaded by

wazzser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
216 views

ZTEST Excel

The document defines classes and modules to create objects for interacting with an Excel spreadsheet using SAP Desktop Office Integration interfaces. Key objects created include a document proxy, spreadsheet interface, and event handler. The modules upload a file, open it in Excel, select sheets and ranges, and insert data.

Uploaded by

wazzser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

*&---------------------------------------------------------------------*

*& Report ZTEST_excel


*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ztest_excel.
* Global definitions
INCLUDE officeintegrationinclude.
DATA:
* OKCODE
gv_okcode TYPE syucomm,
* SAP Desktop Office Integration Interfaces
gc_container TYPE REF TO cl_gui_custom_container,
gc_control TYPE REF TO i_oi_container_control,
gc_document TYPE REF TO i_oi_document_proxy,
gc_spreadsheet TYPE REF TO i_oi_spreadsheet,
gc_error TYPE REF TO i_oi_error,
gt_errors TYPE REF TO i_oi_error OCCURS 0 WITH HEADER LINE,
gv_initialized TYPE c.

*----------------------------------------------------------------------*
* CLASS lcl_excel_event_handler DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_excel_event_handler DEFINITION.
PUBLIC SECTION.
METHODS: on_close_document
FOR EVENT on_close_document OF i_oi_document_proxy
IMPORTING document_proxy has_changed.
ENDCLASS. "lcl_excel_event_handler DEFINITION

DATA lcl_exit TYPE REF TO lcl_excel_event_handler.

*----------------------------------------------------------------------*
* CLASS lcl_excel_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_excel_event_handler IMPLEMENTATION.
METHOD on_close_document.
BREAK-POINT.
ENDMETHOD. "on_close_document
ENDCLASS. "lcl_excel_event_handler IMPLEMENTATION

*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
* Status
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS '100'.
SET TITLEBAR '100'.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*& Module EXIT INPUT
*&---------------------------------------------------------------------*
* Exit program
*----------------------------------------------------------------------*
MODULE exit INPUT.
DATA return_code TYPE i.
DATA: l_rc TYPE t_oi_ret_string.
CASE gv_okcode.
WHEN 'EXIT'.
CALL METHOD cl_gui_cfw=>dispatch
IMPORTING
return_code = return_code.

CALL METHOD gc_control->destroy_control


IMPORTING
retcode = l_rc.
FREE gc_control.
ENDCASE.

ENDMODULE. " EXIT INPUT


*&---------------------------------------------------------------------*
*& Module CREATE_OBJECTS OUTPUT
*&---------------------------------------------------------------------*
* Create Objects
*----------------------------------------------------------------------*
MODULE create_objects OUTPUT.
* Local definitions
TYPES:
t_row(2048) TYPE x. "long byte string

DATA:
* Structure and table to retreive file from Local PC
lw_file_table TYPE file_table,
lt_file_table TYPE filetable,
* Variable for local PC file name
lv_filename TYPE string,
* Table to upload file
lt_upload TYPE STANDARD TABLE OF t_row,
* File length
lv_filelength TYPE i,
* Return code
lv_rc TYPE i,
* Return from Method open_document_from_table
lf_retcode TYPE soi_ret_string,
* Spreadsheet available
lv_spread_available TYPE i.

* First open a dialog window to select file from Local PC


REFRESH lt_file_table.
CLEAR lv_rc.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
EXPORTING
window_title = 'Open'
* DEFAULT_EXTENSION =
default_filename = '*.xls*'
file_filter = '*.xls*'
* WITH_ENCODING =
* INITIAL_DIRECTORY =
* MULTISELECTION =
CHANGING
file_table = lt_file_table
rc = lv_rc
* USER_ACTION =
* FILE_ENCODING =
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc <> 0.
* Implement suitable error handling here
LEAVE PROGRAM.
ENDIF.

* Get the SAP DOI interface references.


* This work has just to be done once !

* First get the SAP DOI i_oi_container_control interface


CALL METHOD c_oi_container_control_creator=>get_container_control
IMPORTING
control = gc_control
error = gc_error.

* Check no errors occured


CALL METHOD gc_error->raise_message
EXPORTING
type = 'E'.

* Create a control container as defined in dynpro 100


CREATE OBJECT gc_container
EXPORTING
container_name = 'CTR_EXCEL'.

* Initialize the SAP DOI Container, tell it to run in the container


* specified above and tell it to run Excel in-place
CALL METHOD gc_control->init_control
EXPORTING
r3_application_name = 'R/3 Basis' "#EC NOTEXT
inplace_enabled = 'X'
inplace_scroll_documents = 'X'
parent = gc_container "gc_container
register_on_close_event = 'X'
register_on_custom_event = 'X'
no_flush = 'X'
IMPORTING
error = gt_errors.

* Save error object in collection


APPEND gt_errors.
* Ask the SAP DOI container for a i_oi_document_proxy for Excel
CALL METHOD gc_control->get_document_proxy
EXPORTING
document_type = 'Excel.Sheet'
no_flush = 'X'
* REGISTER_CONTAINER = 'X'
IMPORTING
document_proxy = gc_document
error = gt_errors.

* Save error object in collection


APPEND gt_errors.

* Upload file from Local PC to SAP


* Read file name user has choosen
CLEAR: lw_file_table, lv_filename.
READ TABLE lt_file_table INTO lw_file_table INDEX 1.
lv_filename = lw_file_table-filename.

REFRESH lt_upload.
CLEAR lv_filelength.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = lv_filename
filetype = 'BIN'
IMPORTING
filelength = lv_filelength
* HEADER =
CHANGING
data_tab = lt_upload
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
not_supported_by_gui = 17
error_no_gui = 18
OTHERS = 19.
IF sy-subrc <> 0.
* Implement suitable error handling here
LEAVE PROGRAM.
ENDIF.

* Now use Method OPEN_DOCUMENT_FROM_TABLE


CLEAR lf_retcode.
CALL METHOD gc_document->open_document_from_table
EXPORTING
document_size = lv_filelength
document_table = lt_upload
document_title = 'Excel inplace'
no_flush = 'X'
open_inplace = 'X'
* OPEN_READONLY = ' '
* PROTECT_DOCUMENT = ' '
* ONSAVE_MACRO = ' '
* STARTUP_MACRO = ''
IMPORTING
error = gt_errors
retcode = lf_retcode.
APPEND gt_errors.

* Check if our document proxy can serve a spreadsheet interface data:


CLEAR lv_spread_available.
CALL METHOD gc_document->has_spreadsheet_interface
EXPORTING
no_flush = 'X'
IMPORTING
is_available = lv_spread_available
error = gt_errors.
APPEND gt_errors.

CALL METHOD gc_document->get_spreadsheet_interface


EXPORTING
no_flush = ' '
IMPORTING
sheet_interface = gc_spreadsheet
error = gt_errors.

* Save error object in collection


APPEND gt_errors.

gc_spreadsheet->select_sheet(
EXPORTING
name = 'septembre' " Name of Worksheet
no_flush = ' ' " Flush?
IMPORTING
error = gt_errors " Error?
retcode = lf_retcode " Text of Error
).

gc_spreadsheet->set_selection(
EXPORTING
left = 1 " Line of Top Left-Hand Cell
top = 60 " Column of Top Left-Hand Cell
rows = 1 " Lines
columns = 50 " Columns
* no_flush = ' ' " Flush?
updating = -1 " Screen Updating
* IMPORTING
* error = " Error?
* retcode = " Text of Error
).
CALL METHOD gc_spreadsheet->insert_range
EXPORTING
name = 'ranges'
rows = '1'
columns = '5'
no_flush = ''
IMPORTING
error = gt_errors.
APPEND gt_errors.

* gc_spreadsheet->hide_rows(
* EXPORTING
* name = 'ranges' " Name of Range
** no_flush = ' ' " Flush?
* IMPORTING
* error = gt_errors " Error?
* retcode = lf_retcode " text
* ).

* CALL METHOD gc_spreadsheet->set_color


* EXPORTING
* rangename = 'ranges'
* front = -1
* back = 6
* no_flush = ' '
* IMPORTING
* error = gt_errors
* retcode = lf_retcode.

* APPEND gt_errors.

LOOP AT gt_errors.
CALL METHOD gt_errors->raise_message
EXPORTING
type = 'E'.
ENDLOOP.
FREE gt_errors.

CREATE OBJECT lcl_exit.


SET HANDLER lcl_exit->on_close_document FOR gc_document.

ENDMODULE. " CREATE_OBJECTS OUTPUT

START-OF-SELECTION.
* Start with the screen
SET SCREEN 100.

You might also like