Upload de arquivo do Excel em ABAP
Autor: Jocelio | Publicado: 2013-04-26T12:29:00-07:00


Olá pessoal!

Trouxe aqui um passo a passo para, em ABAP, desenvolver uma rotina para ler arquivos do EXCEL, inclusive na versão mais recente XLSX.

Vamos logo ao ponto sem muito rodeio.
Defina as seguintes variáveis, itabs etc:


SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
} 
DATA:&nbsp;gt_dados_convertidos&nbsp;&nbsp;TYPE&nbsp;STANDARD&nbsp;TABLE&nbsp;OF&nbsp;tp_excel,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ls_dados_convertidos&nbsp;&nbsp;TYPE&nbsp;tp_excel,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i_tab_raw_data&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TYPE&nbsp;truxs_t_text_data.
DATA:&nbsp;desktop_directory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TYPE&nbsp;string.
DATA:&nbsp;gt_file_tabela&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TYPE&nbsp;STANDARD&nbsp;TABLE&nbsp;OF&nbsp;file_table,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wa_file_tabela&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TYPE&nbsp;file_table,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TYPE&nbsp;i.


Elas serão utilizadas pelos métodos que, em ABAP, a SAP disponibiliza para ler este tipo de arquivo.

Na tela de seleção basta simplismente indicar um campo para o usuário informar em que local e qual o nome do arquivo Excel.


SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}
.L0S70 {
color: #808080;
}

SELECTION-SCREEN&nbsp;BEGIN&nbsp;OF&nbsp;BLOCK&nbsp;cd&nbsp;WITH&nbsp;FRAME&nbsp;TITLE&nbsp;text-s06.
PARAMETERS:&nbsp;filename&nbsp;TYPE&nbsp;localfile&nbsp;OBLIGATORY.
SELECTION-SCREEN&nbsp;END&nbsp;OF&nbsp;BLOCK&nbsp;cd.


Utilizando AT SELECTIONs nós vamos ler o diretório e buscar o arquivo através das linhas abaixo:


SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S31 {
font-style: italic;
color: #808080;
}
.L0S32 {
color: #3399FF;
}
.L0S33 {
color: #4DA619;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}
.L0S70 {
color: #808080;
}

*&nbsp;At&nbsp;selection&nbsp;screen
AT&nbsp;SELECTION-SCREEN&nbsp;ON&nbsp;VALUE-REQUEST&nbsp;FOR&nbsp;filename.

&nbsp;&nbsp;CALL&nbsp;METHOD&nbsp;cl_gui_frontend_services=&gt;get_desktop_directory
&nbsp;&nbsp;&nbsp;&nbsp;CHANGING
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;desktop_directory&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;desktop_directory
&nbsp;&nbsp;&nbsp;&nbsp;EXCEPTIONS
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cntl_error&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error_no_gui&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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sp;(*.XLS)|*.XLS|&nbsp;Excel&nbsp;files&nbsp;(*.XLSX)|*.XLSX|'&nbsp;"cl_gui_frontend_services=&gt;filetype_excel
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;initial_directory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;desktop_directory
&nbsp;&nbsp;&nbsp;&nbsp;CHANGING
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file_table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;gt_file_tabela
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;rc
&nbsp;&nbsp;&nbsp;&nbsp;EXCEPTIONS
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file_open_dialog_failed&nbsp;=&nbsp;1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cntl_error&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;2
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error_no_gui&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;3
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;not_supported_by_gui&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;4
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OTHERS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;5.
&nbsp;&nbsp;IF&nbsp;sy-subrc&nbsp;&lt;&gt;&nbsp;0.
&nbsp;&nbsp;&nbsp;&nbsp;MESSAGE&nbsp;ID&nbsp;sy-msgid&nbsp;TYPE&nbsp;sy-msgty&nbsp;NUMBER&nbsp;sy-msgno
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WITH&nbsp;sy-msgv1&nbsp;sy-msgv2&nbsp;sy-msgv3&nbsp;sy-msgv4.
&nbsp;&nbsp;ELSEIF&nbsp;sy-subrc&nbsp;=&nbsp;0&nbsp;AND&nbsp;rc&nbsp;=&nbsp;1.
&nbsp;&nbsp;&nbsp;&nbsp;READ&nbsp;TABLE&nbsp;gt_file_tabela&nbsp;INTO&nbsp;wa_file_tabela&nbsp;INDEX&nbsp;1&nbsp;.
&nbsp;&nbsp;&nbsp;&nbsp;filename&nbsp;=&nbsp;wa_file_tabela-filename.
&nbsp;&nbsp;ENDIF.



Eu optei por criar um FORM no meu código onde eu incluo o grande milagre desta rotina, a função que converte os dados em ITAB, te livrando de desenvolver linhas e mais linhas para isso.


SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S31 {
font-style: italic;
color: #808080;
}
.L0S32 {
color: #3399FF;
}
.L0S33 {
color: #4DA619;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}
.L0S70 {
color: #808080;
}

FORM&nbsp;excel_process.

&nbsp;&nbsp;CALL&nbsp;FUNCTION&nbsp;'TEXT_CONVERT_XLS_TO_SAP'
&nbsp;&nbsp;&nbsp;&nbsp;EXPORTING
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i_field_seperator&nbsp;&n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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"excel_process


Agora é só chamar o FORM no seu START, END-OF-SELECT.
(lembrando que a tabela final com os dados no nosso exemplo é a gt_dados_convertidos) 


SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S31 {
font-style: italic;
color: #808080;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}

START-OF-SELECTION.

&nbsp;&nbsp;"&nbsp;Arquivo&nbsp;do&nbsp;Excel
&nbsp;&nbsp;PERFORM&nbsp;excel_process.


Um grande abraço a todos, espero ter ajudado