Showing posts with label Read Different Worksheets of Excel. Show all posts
Showing posts with label Read Different Worksheets of Excel. Show all posts

Feb 8, 2011

Read Different Worksheets of Excel


HI this is function Module.

FUNCTION zexcel_lectura_libro .
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     VALUE(FILENAME) LIKE  RLGRAP-FILENAME
*"     VALUE(I_BEGIN_COL) TYPE  I
*"     VALUE(I_BEGIN_ROW) TYPE  I
*"     VALUE(I_END_COL) TYPE  I
*"     VALUE(I_END_ROW) TYPE  I
*"  TABLES
*"      INTERN_SALIDA STRUCTURE  ZALSMEX_TABLINE
*"  EXCEPTIONS
*"      INCONSISTENT_PARAMETERS
*"      UPLOAD_OLE
*"----------------------------------------------------------------------


* Variables Locales
  DATA: l_excel               TYPE ole2_object,
        l_libro               TYPE ole2_object,
        l_hoja                TYPE ole2_object,
        l_cont                TYPE i,
        l_celli               TYPE ole2_object,
        l_cellf               TYPE ole2_object,
        l_cell                TYPE ole2_object,
        lt_tabla              TYPE TABLE OF ty_s_senderline.

  DATA: ld_separator          TYPE  c.

  DATA: ld_rc                 TYPE i.

  DATA: intern                LIKE alsmex_tabline OCCURS 0
                                                  WITH HEADER LINE.

  DATA: lf_pestania(50).



* Se verifica que las filas y columnas sean coherentes
  IF i_begin_row > i_end_row.
    RAISE inconsistent_parameters.
  ENDIF.
  IF i_begin_col > i_end_col.
    RAISE inconsistent_parameters.
  ENDIF.


* Abrimos el Excel
  IF l_excel-header = space OR l_excel-handle = -1.
    CREATE OBJECT l_excel 'Excel.Application'.
  ENDIF.
  CALL METHOD OF l_excel 'Workbooks' = l_libro.
  CALL METHOD OF l_libro 'Open'
    EXPORTING
    #1 = filename.


* Se identifica el separador de campos
  CLASS cl_abap_char_utilities DEFINITION LOAD.
  ld_separator = cl_abap_char_utilities=>horizontal_tab.


* Recorremos las Hojas del Excel
  DO.

*-- Incrementamos el Contador para ir de hoja en hoja
    ADD 1 TO l_cont.

*-- Limpiamos los objetos
    FREE OBJECT: l_cell, l_celli, l_cellf, l_hoja.
    CLEAR lt_tabla[].

*-- Leemos la Hoja
    CALL METHOD OF l_excel 'Worksheets' = l_hoja
      EXPORTING
      #1 = l_cont.

*-- Si no existe la hoja Salimos del Bucle
    IF NOT sy-subrc IS INITIAL.
      EXIT.
    ENDIF.

*   Tomamos el nombre de la pestaсa
    GET PROPERTY OF l_hoja 'NAME' = lf_pestania.
    COMMIT WORK AND WAIT.


*-- Recogemos la Primera Celda
    CALL METHOD OF l_hoja 'Cells' = l_celli
      EXPORTING
      #1 = i_begin_row
      #2 = i_begin_col.
    COMMIT WORK AND WAIT.


*-- Recogemos la Ultima Celda
    CALL METHOD OF l_hoja 'Cells' = l_cellf
      EXPORTING
      #1 = i_end_row
      #2 = i_end_col.
    COMMIT WORK AND WAIT.

*-- Recogemos las celdas comprendidas entre la Primera y la Ultima
*-- y lo copiamos a la memoria intermedia
    CALL METHOD OF l_hoja 'RANGE' = l_cell
      EXPORTING