SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News
Реклама: Играй в інтернет казино.

Export data from Excel



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> OLE2, Excel, WinWord
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1636

PostPosted: Sat Mar 08, 2008 10:59 am    Post subject: Export data from Excel Reply with quote

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.

   AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_CAMI.

     CALL FUNCTION 'WS_FILENAME_GET'
          EXPORTING
               DEF_FILENAME     = ' '
               DEF_PATH         = P_CAMI
               MASK             = ',*.xls.'
               MODE             = 'O'
               TITLE            = 'Archivo a importar'
          IMPORTING
               FILENAME         = P_CAMI
          EXCEPTIONS
               INV_WINSYS       = 01
               NO_BATCH         = 02
               SELECTION_CANCEL = 03
               SELECTION_ERROR  = 04.


   INITIALIZATION.

     P_CAMI = 'C:\PLANTILLAS.XLS'.

     IF P_NLINE IS INITIAL.
       P_NLINE = 1000.
     ENDIF.

* Numero de columnas
     P_NCOLN = 4.


   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.

* 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 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.

   ENDFORM.                               " IMPRIMIR_PLANILHA
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> OLE2, Excel, WinWord All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
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.