Вы находитесь на странице: 1из 9

Criando um BDS:

Para nosso exemplo, vamos criar um EXCEL:

Esse excel vamos chama-lo de ZTESTE.

Na transao OAOR, importar um documento.

Duplo clique (Modo de Tabela):

E escolher o arquivo...

Clique em OK...
Objeto criado:

Para chamar esse template, criar um programa ABAP (se38) com o onte abaixo:
*&---------------------------------------------------------------------*
*& Report ZTESTE_EXCEL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT

zteste_excel.

************************************************************************
**
INSTNCIAS
**
************************************************************************
DATA: v_document
TYPE REF TO i_oi_document_proxy,
v_spreadsheet TYPE REF TO i_oi_spreadsheet,
v_control
TYPE REF TO i_oi_container_control,
l_arq
TYPE string.
DATA: w_conteudo
w_rangesdef
t_conteudo
t_rangesdef

TYPE
TYPE
TYPE
TYPE

soi_generic_item,
soi_dimension_item,
soi_generic_table,
soi_dimension_table.

*----------------------------------------------------------------------*
* Parameters and Select-Options ---------------------------------------*
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS p_nome
TYPE char25.
SELECTION-SCREEN END OF BLOCK b1.
*----------------------------------------------------------------------*
* At Selection-Screen -------------------------------------------------*
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
*----------------------------------------------------------------------*
* Initialization -----------------------------------------------------*
*----------------------------------------------------------------------*
INITIALIZATION.
*----------------------------------------------------------------------*
* Start-Of-Selection --------------------------------------------------*
*----------------------------------------------------------------------*
START-OF-SELECTION.
* Cria Objeto Excel
PERFORM zf_gera_objetos_excel_oaor.
PERFORM zf_abre_layput.
PERFORM zf_atualiza_inf USING:
* nome:
1 2 p_nome.
l_arq = 'c:\temp\teste.xls'.
PERFORM zf_salva_unimed_tm USING l_arq.
END-OF-SELECTION.
*&---------------------------------------------------------------------*
*&
Form ZF_ABRE_LAYPUT
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM zf_abre_layput .
* Types
TYPES: BEGIN OF y_otline,
oleline(128) TYPE c,
END OF y_otline.
* Tabelas Internas
DATA: t_components
t_signature
t_uris
t_oletypes

TYPE
TYPE
TYPE
TYPE

sbdst_components,
sbdst_signature,
sbdst_uri,
TABLE OF y_otline.

* Variveis
DATA: l_doc_format TYPE soi_document_type,
l_version(10) TYPE c VALUE ' '.
* Work Area
DATA: w_signature
w_components
w_uris
* Objetos
DATA: l_bds_instance

TYPE LINE OF sbdst_signature,


TYPE LINE OF sbdst_components,
TYPE LINE OF sbdst_uri.

TYPE REF TO cl_bds_document_set,

l_document_type TYPE soi_document_type VALUE soi_doctype_excel_chart,


l_proxy
TYPE REF TO i_oi_document_proxy,
l_error
TYPE REF TO i_oi_error.
REFRESH: t_signature, t_signature, t_uris.
w_signature-prop_name = 'DESCRIPTION'.
w_signature-prop_value = 'ZTESTE'.
APPEND w_signature TO t_signature.
* Cria Objeto pesquisa URL
IF l_bds_instance IS INITIAL.
CREATE OBJECT l_bds_instance.
ENDIF.
* Retorna Informaes Para Classe e Tipo de Objeto
CALL METHOD l_bds_instance->get_info
EXPORTING
classname
= 'SOFFICEINTEGRATION'
classtype
= 'OT'
object_key
= 'ZTESTE'
CHANGING
components
= t_components
signature
= t_signature
EXCEPTIONS
nothing_found
= 1
error_kpro
= 2
internal_error = 3
parameter_error = 4
not_authorized = 5
not_allowed
= 6.
IF sy-subrc NE 0.
ENDIF.
* Retorna URL para Objeto
CALL METHOD l_bds_instance->get_with_url
EXPORTING
classname
= 'SOFFICEINTEGRATION'
classtype
= 'OT'
object_key
= 'ZTESTE'
CHANGING
uris
= t_uris
signature
= t_signature
EXCEPTIONS
nothing_found
= 1
error_kpro
= 2
internal_error = 3
parameter_error = 4
not_authorized = 5
not_allowed
= 6.
IF sy-subrc NE 0 .
ENDIF.
* Leitura URL Documento
READ TABLE t_components INTO w_components INDEX 1.
READ TABLE t_uris INTO w_uris INDEX 1.
CASE w_components-mimetype.
WHEN 'application/x-rtf' OR 'text/rtf'.
l_doc_format = soi_docformat_rtf.
WHEN 'application/x-oleobject'.
l_doc_format = soi_docformat_compound.
WHEN 'text/plain'.
l_doc_format = soi_docformat_text.
WHEN OTHERS.
l_doc_format = soi_docformat_native.
ENDCASE.
CALL METHOD c_oi_container_control_creator=>mime_to_ole
EXPORTING
mimetype = w_components-mimetype
version = l_version
IMPORTING
oletypes = t_oletypes.
READ TABLE t_oletypes INTO l_document_type INDEX 1.
* Encerra documento
IF NOT v_document IS INITIAL.
CALL METHOD v_document->close_document.
ENDIF.
CALL METHOD v_control->get_document_proxy
EXPORTING
document_type
= l_document_type
document_format
= l_doc_format
register_container = 'X'
IMPORTING
document_proxy
= v_document
error
= l_error.
* caso tenha falhado, exibe a mensagem de erro
IF l_error->has_failed EQ abap_true.
l_error->raise_message( type = 'E' ).

ENDIF.
* Abre URL
CALL METHOD v_document->open_document
EXPORTING
document_url
= w_uris-uri
open_inplace
= 'X'
open_readonly
= 'X'
protect_document = ' '
IMPORTING
error
= l_error.
* caso tenha falhado, exibe a mensagem de erro
IF l_error->has_failed EQ abap_true.
l_error->raise_message( type = 'E' ).
ENDIF.
* Cria Interface com Excel
CALL METHOD v_document->get_spreadsheet_interface
IMPORTING
sheet_interface = v_spreadsheet
error
= l_error.
* caso tenha falhado, exibe a mensagem de erro
IF l_error->has_failed EQ abap_true.
l_error->raise_message( type = 'E' ).
ENDIF.
ENDFORM.

" ZF_ABRE_LAYPUT

*&---------------------------------------------------------------------*
*&
Form zf_atualiza_inf
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_LIN
text
*
-->P_COL
text
*
-->P_VAL
text
*----------------------------------------------------------------------*
FORM zf_atualiza_inf USING
p_lin TYPE any
p_col TYPE any
p_val TYPE any.
* Variveis Locais
DATA: l_val_f TYPE char120.
IF NOT p_val IS INITIAL.
* Formata Campo
WRITE p_val TO l_val_f.
CONDENSE l_val_f.
CLEAR w_conteudo.
w_conteudo-column = p_col.
w_conteudo-row = p_lin.
w_conteudo-value = l_val_f.
APPEND w_conteudo TO t_conteudo.
ENDIF.
ENDFORM.
" ZF_ATUALIZA_INF
*&---------------------------------------------------------------------*
*&
Form zf_salva_unimed_tm
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
*
-->P_ARQ
text
*----------------------------------------------------------------------*
FORM zf_salva_unimed_tm USING p_arq TYPE any.
*--------------------------------------------------------------------*
* Instncias
*--------------------------------------------------------------------*
DATA: l_error
TYPE REF TO i_oi_error.
* Variveis
DATA: l_retcode
TYPE soi_ret_string,
l_file(1000) TYPE c.
* Tabela Interna Local
DATA t_ranges TYPE soi_range_list.
l_file = p_arq.
REFRESH: t_rangesdef.
* Atualiza Sada para Atualizao
LOOP AT t_conteudo INTO w_conteudo.
CLEAR w_rangesdef.
w_rangesdef-rows = 1.
w_rangesdef-columns = 1.
w_rangesdef-row = w_conteudo-row.
w_rangesdef-column = w_conteudo-column.
w_conteudo-row = '1'.

w_conteudo-column = '1'.
MODIFY t_conteudo FROM w_conteudo INDEX sy-tabix.
APPEND w_rangesdef TO t_rangesdef.
ENDLOOP.
* Atualiza Planilha
CALL METHOD v_spreadsheet->set_ranges_data
EXPORTING
ranges
= t_ranges
contents = t_conteudo
rangesdef = t_rangesdef
IMPORTING
retcode
= l_retcode.
CALL METHOD c_oi_errors=>show_message
EXPORTING
type = 'I'.
* Salva Arquivo
CALL METHOD v_document->save_as
EXPORTING
file_name
= l_file
no_flush
= ' '
prompt_user = ' '
IMPORTING
retcode
= l_retcode.
* fecha documento
v_document->close_document(
EXPORTING
do_save = abap_true
IMPORTING
error
= l_error ).
ENDFORM.
" ZF_SALVA_UNIMED_TM
*&---------------------------------------------------------------------*
*&
Form ZF_GERA_OBJETOS_EXCEL_OAOR
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM zf_gera_objetos_excel_oaor .
*--------------------------------------------------------------------*
* Instncias
*--------------------------------------------------------------------*
DATA: l_container
TYPE REF TO cl_gui_custom_container,
l_error
TYPE REF TO i_oi_error.
*--------------------------------------------------------------------*
* Work Area
*--------------------------------------------------------------------*
DATA: w_sheets
TYPE soi_sheets.
*--------------------------------------------------------------------*
* Tabela Interna
*--------------------------------------------------------------------*
DATA: t_sheets
TYPE soi_sheets_table.
REFRESH: t_sheets.
* retorna controle e referncia de erro
c_oi_container_control_creator=>get_container_control(
IMPORTING
control = v_control
error
= l_error ).
* caso tenha falhado, exibe a mensagem de erro
IF l_error->has_failed EQ abap_true.
l_error->raise_message( type = 'E' ).
ENDIF.
* cria container
CREATE OBJECT l_container
EXPORTING
container_name
EXCEPTIONS
cntl_error
cntl_system_error
create_error
lifetime_error
lifetime_dynpro_dynpro_link
OTHERS

= 'CONTAINER'
=
=
=
=
=
=

1
2
3
4
5
6.

IF sy-subrc NE 0.
MESSAGE e368(00) WITH text-007.
ENDIF.
* inicializa controles
v_control->init_control(
EXPORTING
inplace_enabled
= abap_true

r3_application_name
parent
IMPORTING
error
EXCEPTIONS
javabeannotsupported
OTHERS

= 'ExportExcel'
= l_container
= l_error
= 1
= 2 ).

* caso tenha falhado, exibe a mensagem de erro


IF l_error->has_failed EQ abap_true.
l_error->raise_message( type = 'E' ).
ENDIF.
* retorna o proxy para o documento
v_control->get_document_proxy(
EXPORTING
document_type = soi_doctype_excel_sheet
IMPORTING
document_proxy = v_document
error
= l_error ).
* caso tenha falhado, exibe a mensagem de erro
IF l_error->has_failed EQ abap_true.
l_error->raise_message( type = 'E' ).
ENDIF.
* cria documento excel
v_document->create_document(
EXPORTING
document_title = 'ExportExcel'
open_inplace
= abap_true
IMPORTING
error
= l_error ).
* caso tenha falhado, exibe a mensagem de erro
IF l_error->has_failed EQ abap_true.
l_error->raise_message( type = 'E' ).
ENDIF.

ENDFORM.

" ZF_GERA_OBJETOS_EXCEL_OAOR

Resultados:
tela de seleo:

Arquivo gerado: