Categorias

Carga de XLS da Estação de Trabalho para a memória de programa ABAP

Informação fornecida por Marcelo Motta.

Segue um programa que faz upload de XLS.
Para testar crie uma planilha com 5 colunas, sendo que as três últimas tem que ser numéricas.

No código o caminho do arquivo está na variável P_CAMI com o valor C:MEUS DOCUMENTOSTESTE.XLS

REPORT ZEXCEL LINE-SIZE 80 LINE-COUNT 65
NO STANDARD PAGE HEADING.
DATA: BEGIN OF TBXLS OCCURS 5,
LINE LIKE SY-TABIX,
COLN TYPE I,
STRING(1024) TYPE C,
END OF TBXLS,
BEGIN OF TABXLS OCCURS 5,
MATNR(18) TYPE C,
CHARG(10) TYPE C,
VALO1(15) TYPE C,
VALO2(15) TYPE C,
VALO3(15) TYPE C,
POSIC TYPE I,
END OF TABXLS,
WPESO LIKE LIPS-BRGEW,
WVALINT TYPE I,
WTORDEM(20) TYPE C,
WTAMTEX TYPE I,
P_NCOLN LIKE SY-INDEX.
* Ole objects declaration
DATA: H_APPL LIKE OBJ_RECORD,
H_WORK LIKE OBJ_RECORD,
H_CELL LIKE OBJ_RECORD.
INCLUDE OLE2INCL.
INCLUDE DOCSINCL.
SELECTION-SCREEN BEGIN OF BLOCK B0 WITH FRAME TITLE TEXT-001.
PARAMETERS: P_CAMI LIKE RLGRAP-FILENAME. "Arquivo Excel
PARAMETERS: P_NLINE LIKE SY-INDEX. "Numero aproximado de Linhas
SELECTION-SCREEN END OF BLOCK B0.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_CAMI.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
DEF_FILENAME = ' '
DEF_PATH = P_CAMI
MASK = ',*.xls.'
* mask = tmp_mask
MODE = 'O'
TITLE = 'Arquivo a importar !'
IMPORTING
FILENAME = P_CAMI
* RC =
EXCEPTIONS
INV_WINSYS = 01
NO_BATCH = 02
SELECTION_CANCEL = 03
SELECTION_ERROR = 04.

INITIALIZATION.

P_CAMI = 'C:MEUS DOCUMENTOSTESTE.XLS'.

* Se não for informado o número de linhas, iniciar com 100
IF P_NLINE IS INITIAL.
P_NLINE = 100.
ENDIF.
* Número de colunas da planilha.
P_NCOLN = 5.

START-OF-SELECTION.
PERFORM PROCESSA_PLANILHA.
PERFORM IMPRIMIR_PLANILHA.
IF SY-SUBRC = 0.
ENDIF.
* Criar remessas de Exportação.
END-OF-SELECTION.

*&---------------------------------------------------------------------*
*& Form PROCESSA_PLANILHA
*&---------------------------------------------------------------------*
FORM PROCESSA_PLANILHA.

* Start Excel
IF H_APPL-HEADER = SPACE OR H_APPL-HANDLE = -1.
CREATE OBJECT H_APPL 'EXCEL.APPLICATION'.
IF SY-SUBRC NE 0. MESSAGE I002(SY) WITH SY-MSGLI. ENDIF.
SET PROPERTY OF H_APPL 'VISIBLE' = 0.
ENDIF.
* se 'VISIBLE' = 1., o programa abre o EXCEL.

* Open file
CALL METHOD OF H_APPL 'WORKBOOKS' = H_WORK.
CALL METHOD OF H_WORK 'OPEN' EXPORTING #1 = P_CAMI.

* Ler dados da tabela Excel.
PERFORM CAPTURAR_DADOS.

* Release Excel
CALL METHOD OF H_APPL 'QUIT'.
FREE OBJECT H_APPL.
H_APPL-HANDLE = -1.

* Se a primeira linha for comentários (nome dos campos)
* DELETE tbxls WHERE ( string = space ) OR
* ( line = 1 ).
SORT TBXLS BY LINE COLN.
REFRESH TABXLS.
CLEAR TABXLS.
LOOP AT TBXLS.
AT NEW LINE.
CLEAR TABXLS.
ENDAT.
MOVE TBXLS-LINE TO TABXLS-POSIC.
IF ( TBXLS-COLN = 1 ).
MOVE TBXLS-STRING TO TABXLS-MATNR.
ELSEIF ( TBXLS-COLN = 2 ).
MOVE TBXLS-STRING TO TABXLS-CHARG.
ELSEIF ( TBXLS-COLN = 3 ).
MOVE TBXLS-STRING TO WPESO.
MOVE WPESO TO TABXLS-VALO1.
ELSEIF ( TBXLS-COLN = 4 ).
MOVE TBXLS-STRING TO WPESO.
MOVE WPESO TO TABXLS-VALO2.
ELSEIF ( TBXLS-COLN = 5 ).
MOVE TBXLS-STRING TO WPESO.
MOVE WPESO TO TABXLS-VALO3.
ENDIF.
AT END OF LINE.
APPEND TABXLS.
ENDAT.
ENDLOOP.

ENDFORM. " PROCESSA_PLANILHA
*---------------------------------------------------------------------*
* FORM CAPTURAR_DADOS *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM CAPTURAR_DADOS.
DATA: EXCEL_LINE LIKE SY-INDEX,
EXCEL_COLN LIKE SY-INDEX,
CELL_VALUE(132) TYPE C.
DO P_NLINE TIMES.
EXCEL_LINE = SY-INDEX.
* Display indicator
DO P_NCOLN TIMES.
EXCEL_COLN = SY-INDEX.
* Get cell value data
CALL METHOD OF H_APPL 'CELLS' = H_CELL
EXPORTING #1 = EXCEL_LINE
#2 = EXCEL_COLN.
GET PROPERTY OF H_CELL 'VALUE' = CELL_VALUE.
CLEAR: TBXLS.
TBXLS-LINE = EXCEL_LINE.
TBXLS-COLN = EXCEL_COLN.
TBXLS-STRING = CELL_VALUE.
APPEND TBXLS.
ENDDO.
ENDDO.
ENDFORM. " Capturar_dados

*&---------------------------------------------------------------------*
*& Form IMPRIMIR_PLANILHA
*&---------------------------------------------------------------------*
FORM IMPRIMIR_PLANILHA.

WRITE: / 'Material Lote Valor1',
' Valor2 Valor3'.
LOOP AT TABXLS.
WRITE: / TABXLS-MATNR, TABXLS-CHARG, TABXLS-VALO1, TABXLS-VALO2,
TABXLS-VALO3.

ENDLOOP.

ENDFORM. " IMPRIMIR_PLANILHA