W Pub: ABAP Rut Externos XLS

  1. class ZCL_EXCEL_READ DEFINITION.
  2.  
  3.   PUBLIC SECTION.
  4.     data:
  5.         excel_file     type LOCALFILE,
  6.         excel_ini_row  type i,
  7.         excel_ini_col  type i,
  8.         excel_end_row  type i,
  9.         excel_end_col  type i,
  10.         excel_tab      type STANDARD TABLE OF alsmex_tabline,
  11.         excel_tab_head type alsmex_tabline.
  12.  
  13.     METHODS:
  14.        constructor IMPORTING i_filename type any OPTIONAL
  15.                              i_i_row type i OPTIONAL
  16.                              i_i_col type i OPTIONAL
  17.                              i_e_row type i OPTIONAL
  18.                              i_e_col type i OPTIONAL,
  19.  
  20.        READ_EXCEL IMPORTING i_filename TYPE any OPTIONAL.
  21. ENDCLASS.
  22.  
  23. CLASS ZCL_EXCEL_READ IMPLEMENTATION.
  24.  
  25.   METHOD constructor.
  26.     if i_i_row is INITIAL. me->excel_ini_row = 1.     else. me->excel_ini_row = i_i_row. endif.
  27.     if i_i_col is INITIAL. me->EXCEL_INI_COL = 1.     else. me->EXCEL_INI_COL = i_i_col. endif.
  28.     if i_e_col is INITIAL. me->excel_end_col = 100.   else. me->excel_end_col = i_e_col. endif.
  29.     if i_e_row is INITIAL. me->excel_end_row = 65535. else. me->excel_end_row = i_e_row. endif.
  30.     if not i_filename is INITIAL. excel_file = i_filename. endif.
  31.   ENDMETHOD.
  32.  
  33.   METHOD READ_EXCEL.
  34.     if not i_filename is INITIAL.
  35.       me->excel_file = i_filename.
  36.     endif.
  37.     check not me->excel_file is INITIAL.
  38.     CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
  39.       EXPORTING
  40.         filename                = me->excel_file
  41.         i_begin_col             = me->EXCEL_INI_COL
  42.         i_begin_row             = me->EXCEL_INI_ROW
  43.         i_end_col               = me->EXCEL_END_COL
  44.         i_end_row               = me->EXCEL_END_ROW
  45.       TABLES
  46.         intern                  = me->excel_tab
  47.       EXCEPTIONS
  48.         inconsistent_parameters = 1
  49.         upload_ole              = 2
  50.         OTHERS                  = 3.
  51.   ENDMETHOD.
  52.  
  53. ENDCLASS.
  54.  
  55. END-OF-SELECTION.
  56.  
  57. *--[ Ejemplo de USO
  58. data: lv_excel type REF TO ZCL_EXCEL_READ.
  59.  
  60. CREATE OBJECT lv_excel
  61.   EXPORTING
  62.     i_filename = 't:\miexcel.xls'
  63.     i_i_row    = 1
  64.     i_i_col    = 1
  65.     i_e_row    = 100
  66.     i_e_col    = 10.
  67.  
  68. Lv_excel->READ_EXCEL( ).
  69.  
  70. loop at Lv_excel->excel_tab into Lv_excel->excel_tab_head.
  71.   CASE Lv_excel->excel_tab_head-col.
  72.     WHEN '1'. " Procesamos la primera columna
  73.     when '2'. " Procesamos la segunda columna
  74.   endcase.
  75.   AT END OF row.
  76. *    APPEND gt_fact.
  77. *    CLEAR gt_fact.
  78.   ENDAT.
  79. ENDLOOP.