Posted: Sat Mar 08, 2008 10:59 am Post subject: Export data from Excel
Code:
REPORT ZEXCEL LINE-SIZE 170 LINE-COUNT 58
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,
LINEA TYPE I,
CODIGO(10) TYPE C,
NUMLINEA TYPE I,
TEXTO(80),
END OF TABXLS.
DATA: P_NCOLN TYPE I.
* 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.
PARAMETERS: P_CAMI LIKE RLGRAP-FILENAME. "Archivo Excel
PARAMETERS: P_NLINE LIKE SY-INDEX. "Numero aproximado de lineas
SELECTION-SCREEN END OF BLOCK B0.
* 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.
* 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.
* Se a primeira linha for comentarios (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.
IF ( TBXLS-COLN = 1 ).
MOVE TBXLS-STRING TO TABXLS-LINEA.
ELSEIF ( TBXLS-COLN = 2 ).
MOVE TBXLS-STRING TO TABXLS-CODIGO.
ELSEIF ( TBXLS-COLN = 3 ).
MOVE TBXLS-STRING TO TABXLS-NUMLINEA.
ELSEIF ( TBXLS-COLN = 4 ).
MOVE TBXLS-STRING TO TABXLS-TEXTO.
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.
LOOP AT TABXLS.
WRITE: / TABXLS-LINEA, TABXLS-CODIGO, TABXLS-NUMLINEA, TABXLS-TEXTO.
ENDLOOP.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.