You are on page 1of 7

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

*& Report Z_TESTE_BRUNO_EXCEL


*
*&
*
*&---------------------------------------------------------------------*
*&
*
*&
*
*&---------------------------------------------------------------------*
REPORT z_teste_bruno_excel NO STANDARD PAGE HEADING.
DATA:
oref_container
iref_control
iref_document
iref_spreadsheet
iref_error

TYPE
TYPE
TYPE
TYPE
TYPE

REF
REF
REF
REF
REF

DATA:
v_document_url
i_sheets
wa_sheets
i_data
wa_data
i_ranges

TYPE
TYPE
TYPE
TYPE
TYPE
TYPE

c LENGTH 256,
soi_sheets_table,
soi_sheets,
soi_generic_table,
soi_generic_item,
soi_range_list.

DATA:
p_rows
p_cols

TO
TO
TO
TO
TO

cl_gui_custom_container,
i_oi_container_control,
i_oi_document_proxy,
i_oi_spreadsheet,
i_oi_error.

TYPE i VALUE 10,


TYPE i VALUE 6.

RANGES rg_colunas FOR wa_data-column.


TYPES:
BEGIN OF y_cadastro,
nome
TYPE
profissao
TYPE
idade
TYPE
conta
TYPE
banco
TYPE
valor
TYPE
END OF y_cadastro.

char50,
char50,
char3,
char10,
char20,
char20,

DATA:
t_cadastro
wa_cadastro

TYPE TABLE OF y_cadastro,


TYPE y_cadastro.

PARAMETERS:
p_file

TYPE localfile OBLIGATORY.

INITIALIZATION.
CALL METHOD c_oi_container_control_creator=>get_container_control
IMPORTING
control = iref_control
error = iref_error
*
retcode =
.
IF iref_error->has_failed = 'X'.
CALL METHOD iref_error->raise_message
EXPORTING
type = 'E'.

ENDIF.

*
*
*
*
*
*

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

CREATE OBJECT oref_container


EXPORTING
parent
=
container_name
= 'CONT'
style
=
lifetime
= lifetime_default
repid
=
dynnr
=
no_autodef_progid_dynnr
=
EXCEPTIONS
cntl_error
= 1
cntl_system_error
= 2
create_error
= 3
lifetime_error
= 4
lifetime_dynpro_dynpro_link = 5
OTHERS
= 6.
IF sy-subrc <> 0.
MESSAGE e001(00) WITH 'Error while creating container'.
ENDIF.
CALL METHOD iref_control->init_control
EXPORTING
dynpro_nr
= SY-DYNNR
gui_container
= ' '
inplace_enabled
= 'X'
inplace_mode
= 0
inplace_resize_documents = ' '
inplace_scroll_documents = ' '
inplace_show_toolbars
= 'X'
no_flush
= ' '
parent_id
= cl_gui_cfw=>dynpro_0
r3_application_name
= 'EXCEL CONTAINER'
register_on_close_event = ' '
register_on_custom_event = ' '
rep_id
= SY-REPID
shell_style
= 1384185856
parent
= oref_container
name
=
autoalign
= 'x'
IMPORTING
error
= iref_error
retcode
=
EXCEPTIONS
javabeannotsupported
= 1
OTHERS
= 2.
IF iref_error->has_failed = 'X'.
CALL METHOD iref_error->raise_message
EXPORTING
type = 'E'.
ENDIF.

CALL METHOD iref_control->get_document_proxy


EXPORTING
*
document_format
= 'NATIVE'
document_type
= soi_doctype_excel_sheet
*
no_flush
= ' '
*
register_container = ' '

IMPORTING
document_proxy
= iref_document
error
= iref_error
*
retcode
=
.
IF iref_error->has_failed = 'X'.
CALL METHOD iref_error->raise_message
EXPORTING
type = 'E'.
ENDIF.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
* To provide F4 help for the file
PERFORM sub_file_f4.
START-OF-SELECTION.
rg_colunas-sign = 'I'.
rg_colunas-option = 'EQ'.
rg_colunas-low
= '3'.
APPEND rg_colunas.
CONCATENATE 'FILE://' p_file INTO v_document_url.

*
*
*
*
*
*
*

CALL METHOD iref_document->open_document


EXPORTING
document_title = 'Excel'
document_url
= v_document_url
no_flush
= ' '
open_inplace
= 'X'
open_readonly
= ' '
protect_document = ' '
onsave_macro
= ' '
startup_macro
= ''
user_info
=
IMPORTING
error
= iref_error
retcode
=
.
IF iref_error->has_failed = 'X'.
CALL METHOD iref_error->raise_message
EXPORTING
type = 'I'.
LEAVE LIST-PROCESSING.
ENDIF.

CALL METHOD iref_document->get_spreadsheet_interface


EXPORTING
no_flush
= ' '
IMPORTING
error
= iref_error
sheet_interface = iref_spreadsheet
*
retcode
=
.
IF iref_error->has_failed = 'X'.
CALL METHOD iref_error->raise_message
EXPORTING
type = 'I'.

LEAVE LIST-PROCESSING.
ENDIF.
CALL METHOD iref_spreadsheet->get_sheets
EXPORTING
no_flush = ' '
*
updating = -1
IMPORTING
sheets = i_sheets
error
= iref_error
*
retcode =
.
IF iref_error->has_failed = 'X'.
CALL METHOD iref_error->raise_message
EXPORTING
type = 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
DELETE i_sheets WHERE NOT sheet_name = 'TED - Tesouraria'.

*
*

*
*
*

*
*
*

LOOP AT i_sheets INTO wa_sheets.


CALL METHOD iref_spreadsheet->select_sheet
EXPORTING
name
= wa_sheets-sheet_name
no_flush = ' '
IMPORTING
error
= iref_error
retcode =
.
IF iref_error->has_failed = 'X'.
EXIT.
call method iref_error->raise_message
exporting
type = 'E'.
ENDIF.
CALL METHOD iref_spreadsheet->set_selection
EXPORTING
top
= 1
left
= 1
rows
= p_rows
columns = p_cols.
CALL METHOD iref_spreadsheet->insert_range
EXPORTING
name
= 'Test'
rows
= p_rows
columns = p_cols
no_flush = ''
IMPORTING
error
= iref_error.
IF iref_error->has_failed = 'X'.
EXIT.
call method iref_error->raise_message
exporting
type = 'E'.
ENDIF.
REFRESH i_data.

*
*
*

CALL METHOD iref_spreadsheet->get_ranges_data


EXPORTING
no_flush = ' '
all
= 'X'
updating = -1
rangesdef = r_range
IMPORTING
contents = i_data
error
= iref_error
retcode =
CHANGING
ranges
= i_ranges.
DELETE i_data WHERE value IS INITIAL OR value = space.
DELETE i_data WHERE NOT column IN rg_colunas.
ULINE.
WRITE:/1 wa_sheets-sheet_name COLOR 3.
ULINE.
DATA: lc_colunas(4) TYPE c,
lc_num
TYPE p.
CLEAR lc_colunas.
LOOP AT i_data INTO wa_data.
CASE wa_data-column.
WHEN 1.
wa_cadastro-nome
WHEN 2.
wa_cadastro-profissao
WHEN 3.
wa_cadastro-idade
WHEN 4.
wa_cadastro-conta
WHEN 5.
wa_cadastro-banco
WHEN 6.
wa_cadastro-valor
ENDCASE.

= wa_data-value.
= wa_data-value.
= wa_data-value.
= wa_data-value.
= wa_data-value.
= wa_data-value.

AT END OF row.
APPEND wa_cadastro TO t_cadastro.
CLEAR wa_cadastro.
ENDAT.
WRITE:(50) wa_data-value.
AT END OF row.
NEW-LINE.
ENDAT.
ENDLOOP.
ENDLOOP.

LOOP AT t_cadastro INTO wa_cadastro.


IF sy-tabix > 1.
CALL FUNCTION 'MOVE_CHAR_TO_NUM'
EXPORTING
chr
= wa_cadastro-valor
IMPORTING
num
= lc_num
EXCEPTIONS
convt_no_number = 1
convt_overflow = 2
OTHERS
= 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
ENDLOOP.
CALL METHOD iref_document->close_document
* EXPORTING
*
do_save
= ' '
*
no_flush
= ' '
IMPORTING
error
= iref_error
*
has_changed =
*
retcode
=
.
IF iref_error->has_failed = 'X'.
CALL METHOD iref_error->raise_message
EXPORTING
type = 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
CALL METHOD iref_document->release_document
* EXPORTING
*
no_flush = ' '
IMPORTING
error
= iref_error
*
retcode =
.
IF iref_error->has_failed = 'X'.
CALL METHOD iref_error->raise_message
EXPORTING
type = 'I'.
LEAVE LIST-PROCESSING.
ENDIF.
*&---------------------------------------------------------------------*
*&
Form SUB_FILE_F4
*&---------------------------------------------------------------------*
*
F4 help for file path
*----------------------------------------------------------------------*
FORM sub_file_f4 .
DATA:
l_desktop
TYPE string,
l_i_files
TYPE filetable,
l_wa_files
TYPE file_table,
l_rcode
TYPE int4.

* Finding desktop
CALL METHOD cl_gui_frontend_services=>get_desktop_directory
CHANGING
desktop_directory
= l_desktop
EXCEPTIONS
cntl_error
= 1
error_no_gui
= 2
not_supported_by_gui = 3
OTHERS
= 4.
IF sy-subrc <> 0.
MESSAGE e001(00) WITH
'Desktop not found'.
ENDIF.
* Update View
CALL METHOD cl_gui_cfw=>update_view
EXCEPTIONS
cntl_system_error = 1
cntl_error
= 2
OTHERS
= 3.

*
*
*

*
*

CALL METHOD cl_gui_frontend_services=>file_open_dialog


EXPORTING
window_title
= 'Select Excel file'
default_extension
= '.xls'
default_filename
=
file_filter
= '.xls'
with_encoding
=
initial_directory
= l_desktop
multiselection
=
CHANGING
file_table
= l_i_files
rc
= l_rcode
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.
MESSAGE e001(00) WITH 'Erro na abertura do arquivo.'.
ENDIF.
READ TABLE l_i_files INDEX 1 INTO l_wa_files.
IF sy-subrc = 0.
p_file = l_wa_files-filename.
ELSE.
MESSAGE e001(00) WITH 'Erro na abertura do arquivo.'.
ENDIF.

ENDFORM.

" SUB_FILE_F4

You might also like