Aprenda a criar classes no ABAP

O código fonte é uma classe (CLASS) para ser utilizado por programadores, há vários métodos (METHOD) que ajudam o desenvolvedor, principalmente para não ter que ficar reescrevendo ou copiando o código.

*&---------------------------------------------------------------------*
*&  Include           ZCL_ABAP
*&---------------------------------------------------------------------*

*----------------------------------------------------------------------*
*       CLASS CL_ABAP DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_abap DEFINITION.

  PUBLIC SECTION.
*______________________________________________________________________*
*   Declaração global de variaveis, tabelas internas e outros objetos
*   da CLASS
*¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯*
    TYPE-POOLS:
      slis ,
      sydes ,
      ole2 .

    CONSTANTS:
      c_x       TYPE c           VALUE 'X',                 "#EC NOTEXT
      c_blank   TYPE c           VALUE ' ',                 "#EC NOTEXT
      c_true    TYPE boolean     VALUE 'X',                 "#EC NOTEXT
      c_false   TYPE boolean     VALUE ' ',                 "#EC NOTEXT
      c_range   TYPE tvarvc-type VALUE 'S',                 "#EC NOTEXT
      c_param   TYPE tvarvc-type VALUE 'P'.                 "#EC NOTEXT

    TYPES:
      BEGIN OF ty_cols,
        posic    TYPE c LENGTH 002,  " Posição de coluna
      END OF ty_cols ,

      BEGIN OF ty_font,
        code     TYPE c LENGTH 006,  " Definição do nome da fonte
        name     TYPE c LENGTH 020,  " Tipo de fonte (nome)
        size     TYPE i,             " Tamanho da fonte
        bold     TYPE c LENGTH 001,  " Negrito
        italic   TYPE c LENGTH 001,  " Italico
        color    TYPE i,             " Cor da fonte
        bgcolor  TYPE i,             " Cor de fundo
      END OF ty_font ,

      BEGIN OF ty_tabela,
        campo      TYPE c LENGTH 2000,
      END   OF ty_tabela ,

      BEGIN OF ty_arquivo,
        linha         TYPE string,
      END   OF ty_arquivo ,

      ty_campos TYPE TABLE OF string .

    DATA:
      v_ok_code        LIKE sy-ucomm ,
      v_logotype       TYPE c LENGTH 040 VALUE space,     "#EC NOTEXT .
      v_flag           TYPE c LENGTH 001 VALUE space,     "#EC NOTEXT .
      ti_linecolor     TYPE TABLE OF slis_specialcol_alv ,
      ti_sort          TYPE TABLE OF slis_sortinfo_alv ,
      ti_fieldcat      TYPE TABLE OF slis_fieldcat_alv ,
      te_listheader1   TYPE slis_t_listheader ,
      te_listheader    TYPE slis_listheader ,
      te_layout        TYPE slis_layout_alv ,
      te_print         TYPE slis_print_alv ,
      te_variante      TYPE disvariant ,
      te_sort          TYPE slis_sortinfo_alv ,
      te_fieldcat      TYPE slis_fieldcat_alv ,
      ti_listheader    LIKE TABLE OF te_listheader ,
      t_tabela         TYPE TABLE OF ty_tabela ,
      t_campos         TYPE TABLE OF ty_tabela ,
      t_arquivo        TYPE TABLE OF ty_arquivo ,
      vg_tabela        TYPE ty_tabela ,
      vg_arquivo       TYPE ty_arquivo ,
      ti_tvarvc        TYPE TABLE OF tvarvc ,
      wa_tvarvc        TYPE tvarvc ,
      t_cols           TYPE TABLE OF ty_cols ,
      vg_fonts         TYPE ty_font ,
      vg_cols          TYPE ty_cols ,
      vg_excel         TYPE ole2_object ,
      vg_workbook      TYPE ole2_object ,
      vg_plan          TYPE ole2_object ,
      vg_sheets        TYPE ole2_object ,
      vg_cel           TYPE ole2_object ,
      vg_interior      TYPE ole2_object ,
      vg_column        TYPE ole2_object ,
      t_fonts          TYPE TABLE OF ty_font ,
      vg_font          TYPE ole2_object ,
      lv_memo          TYPE c LENGTH 020 VALUE space,
      lv_cursor        TYPE screen-name  VALUE space.
*______________________________________________________________________*
*   Declaração Eventos (Method) da CLASS
*¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯*
    METHODS constructor.

    METHODS set_cursor
      IMPORTING
        !string TYPE string.

    METHODS get_cursor
      IMPORTING
        !memo   TYPE string.

    METHODS gera_xml
      IMPORTING
        value(tabela)     TYPE table
        value(screen_out) TYPE c      DEFAULT space
        value(path)       TYPE string OPTIONAL.

    METHODS move_para_tabela
      IMPORTING
        value(separador) TYPE c DEFAULT ';'
        value(usa_conversor) TYPE c DEFAULT space
      CHANGING
        value(dados) TYPE table .

    METHODS carrega_arquivo
      IMPORTING
        value(diretorio) TYPE string
        value(eliminar_linhas) TYPE numc2 DEFAULT 0
      EXPORTING
        value(subrc) TYPE subrc .

    METHODS salva_arquivo
      IMPORTING
        value(dados) TYPE table
        value(separador) TYPE string DEFAULT ';'
        value(usa_conversor) TYPE c DEFAULT space
        value(diretorio) TYPE string
      EXPORTING
        value(subrc) TYPE subrc .

    METHODS salva_em_csv
      IMPORTING
        value(dados) TYPE table
        value(diretorio) TYPE string
      EXPORTING
        value(subrc) TYPE subrc .

    METHODS salva_em_excel
      IMPORTING
        value(name) TYPE any DEFAULT 'PLAN_DADOS'
        value(dados) TYPE table
        value(usa_conversor) TYPE c DEFAULT space
        value(diretorio) TYPE string
      EXPORTING
        value(subrc) TYPE subrc .

    METHODS carrega_diretorio
      CHANGING
        value(diretorio) TYPE string .

    METHODS carrega_diretorio_in
      CHANGING
        value(diretorio) TYPE string .

    METHODS carrega_diretorio_out
      IMPORTING
        value(nome_do_arquivo)  TYPE string DEFAULT space
        value(tipo_do_arqruivo) TYPE string DEFAULT '.csv'
      CHANGING
        value(diretorio) TYPE string .

    METHODS carregar_data
      IMPORTING
        value(_name) TYPE string
      EXPORTING
        value(_value) TYPE any
        value(_subrc) TYPE subrc .

    METHODS carregar_range
      IMPORTING
        value(_name) TYPE string
      EXPORTING
        value(_range) TYPE table
        value(_subrc) TYPE subrc .

    METHODS processando
      IMPORTING
        !texto TYPE string OPTIONAL .

    METHODS reset_alv .

    METHODS add_fieldcat
      IMPORTING
        !i_string TYPE string .

    METHODS add_sort
      IMPORTING
        !i_string TYPE string .

    METHODS add_header
      IMPORTING
        !i_string TYPE string .

    METHODS f4_variants
      CHANGING
        !p_variant TYPE slis_vari .

    METHODS display_report
      IMPORTING
        !i_repid TYPE sy-repid
        !i_optimize TYPE c DEFAULT space
        !i_edit TYPE c DEFAULT space
        !i_print_info TYPE c DEFAULT 'X'
        !i_variant TYPE slis_vari DEFAULT space
        !i_pf_status TYPE slis_formname DEFAULT space
        !i_top_of_page TYPE slis_formname DEFAULT space
        !i_exit_top_of_page TYPE slis_formname DEFAULT space
        !i_exit_end_of_page TYPE slis_formname DEFAULT space
        !i_user_command TYPE slis_formname DEFAULT space
        !i_title TYPE lvc_title DEFAULT space
        !i_start_col TYPE i DEFAULT 0
        !i_start_row TYPE i DEFAULT 0
        !i_end_col TYPE i DEFAULT 0
        !i_end_row TYPE i DEFAULT 0
        !i_fieldname TYPE slis_fieldname DEFAULT space
        !i_tabname TYPE slis_tabname DEFAULT space
        !i_table TYPE table .

    METHODS open_excel
      IMPORTING
        value(name) TYPE any
        value(diretorio) TYPE string
      EXPORTING
        value(subrc) TYPE subrc .

    METHODS set_font
      IMPORTING
        value(code) TYPE any DEFAULT 'DATA'
        value(name) TYPE any DEFAULT 'Arial'
        value(size) TYPE any DEFAULT 10
        value(bold) TYPE any DEFAULT ' '
        value(italic) TYPE any DEFAULT ' '
        value(color) TYPE any DEFAULT 0
        value(bgcolor) TYPE any DEFAULT -4142 .

    METHODS use_font
      IMPORTING
        !format TYPE any .

    METHODS write_cell
      IMPORTING
        value(format) TYPE any DEFAULT 'DATA'
        value(row) TYPE any
        value(col) TYPE any
        value(value) TYPE any OPTIONAL
      EXPORTING
        value(subrc) TYPE subrc .

    METHODS freeze
      IMPORTING
        value(row) TYPE i
        value(col) TYPE i
      EXPORTING
        !subrc TYPE subrc .

    METHODS close_excel
      IMPORTING
        value(diretorio) TYPE string .

ENDCLASS.                    "cl_abap  DEFINITIO

*----------------------------------------------------------------------*
*       CLASS CL_ABAP IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_abap IMPLEMENTATION.

* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD constructor.

    DATA v_len TYPE i            VALUE 0.
    DATA v_c   TYPE c LENGTH 001 VALUE space.
    DATA v_n   TYPE n LENGTH 002 VALUE 0.

    FIELD-SYMBOLS: <t_cols>  TYPE ty_cols.

    v_len = STRLEN( sy-abcde ).

    CLEAR t_cols[].

* Cria as posições de colunas de "A" a "Z"
    DO v_len TIMES.

      v_n = sy-index - 1.

* Pega a letra referida (Ex.: "B" )
      APPEND INITIAL LINE TO t_cols ASSIGNING <t_cols>.
      <t_cols>-posic = sy-abcde+v_n(1).

    ENDDO.

* Cria as posições de colunas de "AA" até "ZZ"
    DO v_len TIMES.

      v_n = sy-index - 1.

* Pega a letra referida (Ex.: "B" )
      v_c = sy-abcde+v_n(1).

      DO v_len TIMES.

        v_n = sy-index - 1.

        APPEND INITIAL LINE TO t_cols ASSIGNING <t_cols>.

* Adiciona a segunda letra referida (Ex.: "BX")
        CONCATENATE v_c sy-abcde+v_n(1) INTO <t_cols>-posic.

      ENDDO.

    ENDDO.
*--> Resultado final é as colunas de: "A" até "ZZ"

*--> Define cinco tipos de formatos:
* Título
    me->set_font( EXPORTING code    = 'TITLE'
                            name    = 'Tahoma'
                            size    = 18
                            bold    = 'X'
                            italic  = 'X'
                            color   = 2
                            bgcolor = 41 ).

* Cabeçalho
    me->set_font( EXPORTING code    = 'HEADER'
                            name    = 'Arial'
                            size    = 14
                            bold    = ' '
                            italic  = 'X'
                            color   = 0
                            bgcolor = 40 ).

* Dados
    me->set_font( EXPORTING code    = 'KEY'
                            name    = 'Courier'
                            size    = 10
                            bold    = 'X'
                            italic  = ' '
                            color   = 1
                            bgcolor = 4 ).

    me->set_font( EXPORTING code    = 'DATA'
                            name    = 'Courier'
                            size    = 10
                            bold    = 'X'
                            italic  = ' '
                            color   = 0 ""41
                            bgcolor = -4142 ).

* Subtotal
    me->set_font( EXPORTING code    = 'SUB'
                            name    = 'Times New Roman'
                            size    = 10
                            bold    = ' '
                            italic  = ' '
                            color   = 1
                            bgcolor = 36 ).

* Total.
    me->set_font( EXPORTING code    = 'TOTAL'
                            name    = 'Times New Roman'
                            size    = 10
                            bold    = 'X'
                            italic  = ' '
                            color   = 1
                            bgcolor = 6 ).

  ENDMETHOD.                    "constructor

* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->SET_CURSOR
* +-------------------------------------------------------------------------------------------------+
* | [--->] STRING                         TYPE        STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD set_cursor.

    CLEAR: lv_memo, lv_cursor.

    SPLIT string AT '|' INTO lv_memo lv_cursor.
    IF ( NOT ( lv_memo   IS INITIAL ) AND
         NOT ( lv_cursor IS INITIAL ) ).

      FREE MEMORY ID lv_memo.

      EXPORT m_cursor = lv_cursor TO MEMORY ID lv_memo.
      MESSAGE s055(00) DISPLAY LIKE 'E'.

    ENDIF.

  ENDMETHOD.                    "set_cursor

* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->GET_CURSOR
* +-------------------------------------------------------------------------------------------------+
* | [--->] MEMO                           TYPE        STRING
* | [<-->] XML                            TYPE        TABLE
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD get_cursor.

    CLEAR: lv_memo, lv_cursor.

    lv_memo = memo.

    IMPORT m_cursor = lv_cursor FROM MEMORY ID lv_memo.
    IF ( lv_cursor <> space ).
      SET CURSOR FIELD lv_cursor.
    ENDIF.

    FREE MEMORY ID lv_memo.

  ENDMETHOD.                    "get_cursor

* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->GERA_XML
* +-------------------------------------------------------------------------------------------------+
* | [--->] TABELA                         TYPE        TABLE
* | [<-->] XML                            TYPE        TABLE
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD gera_xml.

*>>> Declaração de Variavei, Tab.Interna e outros Objetos Locais
    TYPES:
      BEGIN OF ty_screen,
        line         TYPE c LENGTH 2000,
      END   OF ty_screen.

    DATA:
      vl_from        TYPE string,
      vl_to          TYPE string,
      vl_string      TYPE string,
      vl_tabix       TYPE sy-tabix,
      vl_campos      TYPE ty_tabela,
      vl_type        TYPE c LENGTH 001,
      vl_line        TYPE n LENGTH 015,
      vl_data        TYPE c LENGTH 010,
      vl_hora        TYPE c LENGTH 008,
      vl_pos         TYPE n LENGTH 001,
      vl_names       TYPE sydes_nameinfo,
      tl_xml         TYPE TABLE OF string,
      tl_screen      TYPE TABLE OF ty_screen,
      tl_table       TYPE sydes_desc.

    FIELD-SYMBOLS:
      <t_xml>        TYPE ANY,
      <tabela>       TYPE ANY,
      <vl_from>      TYPE ANY,
      <vl_to>        TYPE ANY,
      <tl_screen>    LIKE LINE OF tl_screen.

    IF ( screen_out = space AND path IS INITIAL ).
      MESSAGE s398(00) WITH 'Falta o caminho' '(diretório) e' 'arquivo!' space DISPLAY LIKE 'E'.
      RETURN.
    ENDIF.

    GET TIME.
    WRITE sy-datum TO vl_data USING EDIT MASK '__/__/____'.
    WRITE sy-uzeit TO vl_hora USING EDIT MASK '__:__:__'.

*>>> Lista os campos da tabela interna do parâmetro de entrada
    FREE tl_table.
    DESCRIBE FIELD tabela INTO tl_table.
    IF ( tl_table IS INITIAL ).
      RETURN.
    ENDIF.

    DELETE tl_table-names WHERE name+0(2) = '=='.
    DELETE ADJACENT DUPLICATES FROM tl_table-names
                          COMPARING name.
    DELETE tl_table-names FROM 1 TO 2.

    CLEAR: t_campos[].
    LOOP AT tl_table-names INTO vl_names.

      SEARCH vl_names-name FOR '-' IN CHARACTER MODE.
      CHECK ( sy-subrc <> 0 ).

      APPEND vl_names-name TO t_campos.

    ENDLOOP.

    IF ( t_campos[] IS INITIAL ).
      RETURN.
    ENDIF.

*>>> Adiciona a TAG de identificação do arquivo
    APPEND INITIAL LINE TO tl_xml ASSIGNING <t_xml>.
    CONCATENATE
      '<SAP_file_xml xmlns:SAP="'
      path
      sy-sysid ':/'
      sy-saprl '/'
      sy-mandt ':'
      sy-opsys ':'
      vl_data '">' INTO <t_xml>.

*>>> Adiciona a TAG de início de arquivo
    APPEND INITIAL LINE TO tl_xml ASSIGNING <t_xml>.
    <t_xml> ='<FILE_XML>'.

    LOOP AT tabela ASSIGNING <tabela>.

      vl_line = sy-tabix.

*>>> Adiciona a TAG de início de registro
      APPEND INITIAL LINE TO tl_xml ASSIGNING <t_xml>.
      CONCATENATE '<R' vl_line '>' INTO <t_xml>.

*>>> Processo dinâmico da captura da campos
      LOOP AT t_campos INTO vl_campos.

        vl_tabix = sy-tabix.

        CLEAR vl_from.
        vl_from = vl_campos.

        CLEAR vl_from.
        CONCATENATE '<TABELA>-' vl_campos-campo INTO vl_from.
        CONDENSE vl_from NO-GAPS.

        ASSIGN (vl_from) TO <vl_from>.
        CHECK ( <vl_from> IS ASSIGNED ).

        vl_to = <vl_from>.
        UNASSIGN <vl_from>.

*>>> Adiciona os dados ao XML
        APPEND INITIAL LINE TO tl_xml ASSIGNING <t_xml>.
        CONCATENATE <t_xml> '<' vl_campos '>' vl_to '</' vl_campos '>' INTO <t_xml>.

      ENDLOOP.  "<<< t_tabela[]

      APPEND INITIAL LINE TO tl_xml ASSIGNING <t_xml>.
      CONCATENATE '</R' vl_line '>' INTO <t_xml>.

    ENDLOOP.  "<<< ti_arquivo[]

    APPEND INITIAL LINE TO tl_xml ASSIGNING <t_xml>.
    <t_xml> = '</FILE_XML>'.

    APPEND INITIAL LINE TO tl_xml ASSIGNING <t_xml>.
    <t_xml> = '</SAP_file_xml>'.

    IF NOT ( tl_xml[] IS INITIAL ).

      IF ( screen_out = space ).

        CALL FUNCTION 'GUI_DOWNLOAD'
          EXPORTING
            filename                = path
            filetype                = 'ASC'
          TABLES
            data_tab                = tl_xml
          EXCEPTIONS
            file_write_error        = 1
            no_batch                = 2
            gui_refuse_filetransfer = 3
            invalid_type            = 4
            no_authority            = 5
            unknown_error           = 6
            header_not_allowed      = 7
            separator_not_allowed   = 8
            filesize_not_allowed    = 9
            header_too_long         = 10
            dp_error_create         = 11
            dp_error_send           = 12
            dp_error_write          = 13
            unknown_dp_error        = 14
            access_denied           = 15
            dp_out_of_memory        = 16
            disk_full               = 17
            dp_timeout              = 18
            file_not_found          = 19
            dataprovider_exception  = 20
            control_flush_error     = 21
            OTHERS                  = 22.
        IF sy-subrc <> 0.
          MESSAGE ID sy-msgid
                TYPE 'S'
              NUMBER sy-msgno
                WITH sy-msgv1
                     sy-msgv2
                     sy-msgv3
                     sy-msgv4 DISPLAY LIKE sy-msgty.
        ENDIF.

      ELSE.

        vl_pos = 2.
        LOOP AT tl_xml INTO vl_string.

          vl_tabix = sy-tabix.

          APPEND INITIAL LINE TO tl_screen ASSIGNING <tl_screen>.
          CASE vl_tabix.

            WHEN 1.
              <tl_screen>-line = vl_string.

            WHEN 2.
              <tl_screen>-line+vl_pos = vl_string.
              ADD 3 TO vl_pos.

            WHEN 3.
              <tl_screen>-line+vl_pos = vl_string.
              ADD 3 TO vl_pos.

            WHEN OTHERS.
              IF ( vl_string+0(2) = '</' ).
                IF ( vl_pos = 2 ).
                  <tl_screen>-line = vl_string.
                ELSE.
                  SUBTRACT 3 FROM vl_pos.
                  <tl_screen>-line+vl_pos = vl_string.
                ENDIF.
              ELSE.
                <tl_screen>-line+vl_pos = vl_string.
                IF ( vl_pos = 5 ).
                  ADD 3 TO vl_pos.
                ENDIF.
              ENDIF.

          ENDCASE.

        ENDLOOP.

        EDITOR-CALL FOR tl_screen TITLE 'Arquivo XML' DISPLAY-MODE.

      ENDIF.

    ENDIF.

  ENDMETHOD.                    "gera_xml

* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->ADD_FIELDCAT
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_STRING                       TYPE        STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD add_fieldcat.

    DATA:
      vl_string    TYPE string     VALUE space,
      vl_table     TYPE string     VALUE space,
      vl_field     TYPE string     VALUE space,
      vl_curow     TYPE string,
      vl_cucol     TYPE n LENGTH 003 VALUE 0.
*      te_fieldcat  LIKE slis_t_fieldcat_alv.

    CLEAR:
      vl_string,
      vl_table,
      vl_field,
      te_fieldcat.

    DESCRIBE TABLE ti_fieldcat LINES vl_cucol.
    IF ( vl_cucol = 0 ).
      vl_cucol = 1.
    ENDIF.

    SPLIT i_string
       AT '|'
     INTO vl_cucol                 "... Posição da Coluna
          te_fieldcat-fix_column   "... Coluna Fixa
          vl_string                "... Tabela e Campo
          te_fieldcat-inttype      "... Tipo
          te_fieldcat-outputlen    "... Tamanho da Coluna
          te_fieldcat-just         "... Alinhamento
          te_fieldcat-reptext_ddic "... Titulo da Coluna
          te_fieldcat-hotspot      "... Coluna Secitiva
          te_fieldcat-checkbox     "... Coluna de Marcação
          te_fieldcat-icon         "... Coluna de Icone
          te_fieldcat-symbol       "... Coluna de Simbolo
          te_fieldcat-do_sum       "... Coluna com Total
          te_fieldcat-no_out       "... Coluna Oculta
          te_fieldcat-edit.        "... Coluna Editavel

    SPLIT vl_string AT '-' INTO vl_table vl_field.

    te_fieldcat-row_pos   = vl_curow. "... Posição da Linha
    te_fieldcat-col_pos   = vl_cucol. "... Posição da Coluna
    te_fieldcat-tabname   = vl_table. "... Tabela
    te_fieldcat-fieldname = vl_field. "... Campo

*    "... Quando for uma coluna de edição é uma entrada
*    IF ( te_fieldcat-edit <> space ).
*      te_fieldcat-input = 'X'.
*    ENDIF.

    APPEND te_fieldcat TO ti_fieldcat.

  ENDMETHOD.                    "add_fieldcat


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->ADD_HEADER
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_STRING                       TYPE        STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD add_header.

    CLEAR te_listheader.

    SPLIT i_string
       AT '|'
     INTO te_listheader-typ
          te_listheader-key
          te_listheader-info.

    APPEND te_listheader TO ti_listheader.

  ENDMETHOD.                    "add_header


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->ADD_SORT
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_STRING                       TYPE        STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD add_sort.

    DATA:
      vl_string  TYPE string VALUE space,
      vl_table   TYPE string VALUE space,
      vl_field   TYPE string VALUE space.

    CLEAR:
      vl_string,
      vl_table,
      vl_field,
      te_sort.

    SPLIT i_string
       AT '|'
     INTO te_sort-spos            "... Posição de Ordenação
          vl_string               "... Tabela e Campo
          te_sort-up              "... Ordenação Cresente
          te_sort-down            "... Ordenação Decresente
          te_sort-group           "... Agrupamento pelo Campo
          te_sort-subtot.         "... Coluna de SubTotais

    SPLIT vl_string AT '-' INTO vl_table vl_field.

    te_sort-tabname   = vl_table. "... Tabela
    te_sort-fieldname = vl_field. "... Campo

    APPEND te_sort TO ti_sort.

  ENDMETHOD.                    "add_sort


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->CARREGAR_DATA
* +-------------------------------------------------------------------------------------------------+
* | [--->] _NAME                          TYPE        STRING
* | [<---] _VALUE                         TYPE        ANY
* | [<---] _SUBRC                         TYPE        SUBRC
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD carregar_data.

    SELECT SINGLE * INTO wa_tvarvc
                    FROM tvarvc
                   WHERE name = _name
                     AND type = c_param.
    IF sy-subrc <> 0.
      MESSAGE s398(00)
         WITH 'Parâmetro' _name 'não encontrado' space
         DISPLAY LIKE 'E'.
      _subrc = 4.
      EXIT.
    ENDIF.

    _subrc = 0.

    _value = wa_tvarvc-low.

  ENDMETHOD.                    "CARREGAR_DATA


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->CARREGAR_RANGE
* +-------------------------------------------------------------------------------------------------+
* | [--->] _NAME                          TYPE        STRING
* | [<---] _RANGE                         TYPE        TABLE
* | [<---] _SUBRC                         TYPE        SUBRC
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD carregar_range.

    DATA d_ref TYPE REF TO data.

    FIELD-SYMBOLS: <_range> TYPE ANY,
                   <_field> TYPE ANY.

    CREATE DATA d_ref LIKE LINE OF _range.
    ASSIGN d_ref->* TO <_range>.

    SELECT * INTO TABLE ti_tvarvc
                   FROM tvarvc
                  WHERE name = _name
                    AND type = c_range.
    IF ( ti_tvarvc[] IS INITIAL ).
      MESSAGE s398(00)
         WITH 'Parâmetro' _name 'não encontrado' space
         DISPLAY LIKE 'E'.
      _subrc = 4.
      EXIT.
    ENDIF.

    _subrc = 0.

    LOOP AT ti_tvarvc INTO wa_tvarvc.

      ASSIGN COMPONENT 'SIGN' OF STRUCTURE <_range> TO <_field>.
      IF sy-subrc = 0.
        <_field> = wa_tvarvc-sign.
      ENDIF.

      ASSIGN COMPONENT 'OPTION' OF STRUCTURE <_range> TO <_field>.
      IF sy-subrc = 0.
        <_field> = wa_tvarvc-opti.
      ENDIF.

      ASSIGN COMPONENT 'LOW' OF STRUCTURE <_range> TO <_field>.
      IF sy-subrc = 0.
        <_field> = wa_tvarvc-low.
      ENDIF.

      ASSIGN COMPONENT 'HIGH' OF STRUCTURE <_range> TO <_field>.
      IF sy-subrc = 0.
        <_field> = wa_tvarvc-high.
      ENDIF.

      APPEND <_range> TO _range.

    ENDLOOP.

  ENDMETHOD.                    "CARREGAR_RANGE


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->CARREGA_ARQUIVO
* +-------------------------------------------------------------------------------------------------+
* | [--->] DIRETORIO                      TYPE        STRING
* | [--->] ELIMINAR_LINHAS                TYPE        NUMC2 (default =0)
* | [<---] SUBRC                          TYPE        SUBRC
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD carrega_arquivo.

    CLEAR t_arquivo[].
    CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
        filename                = diretorio
        filetype                = 'ASC'
      TABLES
        data_tab                = t_arquivo[]
      EXCEPTIONS
        file_open_error         = 1
        file_read_error         = 2
        no_batch                = 3
        gui_refuse_filetransfer = 4
        invalid_type            = 5
        no_authority            = 6
        unknown_error           = 7
        bad_data_format         = 8
        header_not_allowed      = 9
        separator_not_allowed   = 10
        header_too_long         = 11
        unknown_dp_error        = 12
        access_denied           = 13
        dp_out_of_memory        = 14
        disk_full               = 15
        dp_timeout              = 16
        OTHERS                  = 17.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid
            TYPE 'S'
          NUMBER sy-msgno
            WITH sy-msgv1
                 sy-msgv2
                 sy-msgv3
                 sy-msgv4 DISPLAY LIKE sy-msgty.
      subrc = 4.
      EXIT.
    ENDIF.

    IF ( t_arquivo[] IS INITIAL ).
      MESSAGE s208(00)
         WITH 'Não ha dados para carga!' DISPLAY LIKE 'E'.
      subrc = 4.
      EXIT.
    ENDIF.

*>>> Elimina as 5 primeiras linhas da tabela interna
    IF ( eliminar_linhas > 0 ).
      DELETE t_arquivo FROM 1 TO eliminar_linhas.
    ENDIF.

    IF ( t_arquivo[] IS INITIAL ).
      MESSAGE s208(00)
         WITH 'Não ha dados para carga!' DISPLAY LIKE 'E'.
      subrc = 4.
      EXIT.
    ENDIF.

    subrc = 0.

  ENDMETHOD.                    "carrega_arquivo


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->CARREGA_DIRETORIO
* +-------------------------------------------------------------------------------------------------+
* | [<-->] DIRETORIO                      TYPE        STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD carrega_diretorio.

    DATA:
      vl_rc          TYPE i,
      tl_file        TYPE TABLE OF file_table,
      vl_file        TYPE file_table.

    IF ( diretorio IS INITIAL ).
      CALL METHOD cl_gui_frontend_services=>get_sapgui_workdir
        CHANGING
          sapworkdir = diretorio.
    ENDIF.

    CALL METHOD cl_gui_frontend_services=>directory_browse
      EXPORTING
        window_title         = 'Diretórios'
        initial_folder       = diretorio
      CHANGING
        selected_folder      = diretorio
      EXCEPTIONS
        cntl_error           = 1
        error_no_gui         = 2
        not_supported_by_gui = 3
        OTHERS               = 4.
    IF ( sy-subrc EQ 0 ).
      CONCATENATE diretorio '' INTO diretorio.
    ENDIF.

  ENDMETHOD.                    "CARREGA_DIRETORIO_IN


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->CARREGA_DIRETORIO_IN
* +-------------------------------------------------------------------------------------------------+
* | [<-->] DIRETORIO                      TYPE        STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD carrega_diretorio_in.

    DATA:
      vl_rc          TYPE i,
      tl_file        TYPE TABLE OF file_table,
      vl_file        TYPE file_table.

    IF ( diretorio IS INITIAL ).
      CALL METHOD cl_gui_frontend_services=>get_sapgui_workdir
        CHANGING
          sapworkdir = diretorio.
    ENDIF.

    CALL METHOD cl_gui_frontend_services=>file_open_dialog
      EXPORTING
        window_title            = 'Arquivo de carga de dados'
        default_extension       = '.txt'
        file_filter             = '*.*'
        initial_directory       = diretorio
      CHANGING
        file_table              = tl_file[]
        rc                      = vl_rc
      EXCEPTIONS
        file_open_dialog_failed = 1
        cntl_error              = 2
        error_no_gui            = 3
        OTHERS                  = 4.
    IF sy-subrc = 0.

      READ TABLE tl_file INTO vl_file INDEX 1.
      CHECK sy-subrc IS INITIAL.
      MOVE vl_file-filename TO diretorio.

    ENDIF.

  ENDMETHOD.                    "CARREGA_DIRETORIO_IN


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->CARREGA_DIRETORIO_OUT
* +-------------------------------------------------------------------------------------------------+
* | [--->] NOME_DO_ARQUIVO                TYPE        STRING (default =SPACE)
* | [--->] TIPO_DO_ARQRUIVO               TYPE        STRING (default =.CSV
* | [<-->] DIRETORIO                      TYPE        STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD carrega_diretorio_out.

    DATA:
      tipo             TYPE  string,
      filename         TYPE  string,
      path             TYPE  string,
      fullpath         TYPE  string,
      workdir          TYPE  string,
      user_action      TYPE  i.

    IF ( diretorio IS INITIAL ).
      CALL METHOD cl_gui_frontend_services=>get_sapgui_workdir
        CHANGING
          sapworkdir = diretorio.
    ENDIF.

    IF ( nome_do_arquivo IS INITIAL ).
      CONCATENATE 'D'
                  sy-datum
                  'H'
                  sy-uzeit
             INTO nome_do_arquivo.
    ENDIF.

    CONCATENATE '*' tipo_do_arqruivo INTO tipo.

    CONCATENATE nome_do_arquivo tipo_do_arqruivo INTO filename.
    path = diretorio.

    CALL METHOD cl_gui_frontend_services=>file_save_dialog
      EXPORTING
        window_title         = 'Download de dados'
        initial_directory    = workdir
        default_extension    = tipo
        default_file_name    = filename
        file_filter          = tipo
        prompt_on_overwrite  = 'X'
      CHANGING
        filename             = filename
        path                 = path
        fullpath             = fullpath
        user_action          = user_action
      EXCEPTIONS
        cntl_error           = 1
        error_no_gui         = 2
        not_supported_by_gui = 3
        OTHERS               = 4.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid
            TYPE 'S'
          NUMBER sy-msgno
            WITH sy-msgv1
                 sy-msgv2
                 sy-msgv3
                 sy-msgv4 DISPLAY LIKE sy-msgty.
      EXIT.
    ENDIF.

    IF user_action <> cl_gui_frontend_services=>action_ok.
      MESSAGE s208(00) WITH 'Ação não pode ser executada!' DISPLAY LIKE 'E'.
      EXIT.
    ENDIF.

    diretorio = fullpath.

  ENDMETHOD.                    "CARREGA_DIRETORIO_OUT


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->CLOSE_EXCEL
* +-------------------------------------------------------------------------------------------------+
* | [--->] DIRETORIO                      TYPE        STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD close_excel.

*** Exibe o report gerado
    SET PROPERTY OF vg_excel 'Visible' = 0.

    GET PROPERTY OF vg_excel 'ActiveWorkbook' = vg_workbook.

    CALL METHOD OF vg_workbook 'SaveAs' EXPORTING #1 = diretorio.
    CALL METHOD OF vg_excel 'Quit'.

*** Libera da memória os objetos criados.
    FREE OBJECT vg_sheets.
    FREE OBJECT vg_workbook.
    FREE OBJECT vg_excel.

    CLEAR: vg_sheets, vg_workbook, vg_excel.

  ENDMETHOD.                    "close_excel


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->DISPLAY_REPORT
* +-------------------------------------------------------------------------------------------------+
* | [--->] I_REPID                        TYPE        SY-REPID
* | [--->] I_OPTIMIZE                     TYPE        C (default =SPACE)
* | [--->] I_EDIT                         TYPE        C (default =SPACE)
* | [--->] I_PRINT_INFO                   TYPE        C (default ='X')
* | [--->] I_VARIANT                      TYPE        SLIS_VARI (default =SPACE)
* | [--->] I_PF_STATUS                    TYPE        SLIS_FORMNAME (default =SPACE)
* | [--->] I_TOP_OF_PAGE                  TYPE        SLIS_FORMNAME (default =SPACE)
* | [--->] I_EXIT_TOP_OF_PAGE             TYPE        SLIS_FORMNAME (default =SPACE)
* | [--->] I_EXIT_END_OF_PAGE             TYPE        SLIS_FORMNAME (default =SPACE)
* | [--->] I_USER_COMMAND                 TYPE        SLIS_FORMNAME (default =SPACE)
* | [--->] I_TITLE                        TYPE        LVC_TITLE (default =SPACE)
* | [--->] I_START_COL                    TYPE        I (default =0)
* | [--->] I_START_ROW                    TYPE        I (default =0)
* | [--->] I_END_COL                      TYPE        I (default =0)
* | [--->] I_END_ROW                      TYPE        I (default =0)
* | [--->] I_FIELDNAME                    TYPE        SLIS_FIELDNAME (default =SPACE)
* | [--->] I_TABNAME                      TYPE        SLIS_TABNAME (default =SPACE)
* | [--->] I_TABLE                        TYPE        TABLE
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD display_report.

    CLEAR v_flag.

    te_layout-expand_all        = 'X'.
    te_layout-colwidth_optimize = i_optimize.
    te_layout-edit              = i_edit.
    te_layout-box_fieldname     = i_fieldname.
    te_layout-box_tabname       = i_tabname.

    te_variante-variant         = i_variant.

    te_print-no_print_listinfos = i_print_info.

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_callback_program          = i_repid
        i_callback_top_of_page      = i_top_of_page
        i_callback_html_top_of_page = i_exit_top_of_page
        i_callback_html_end_of_list = i_exit_end_of_page
        i_callback_pf_status_set    = i_pf_status
        i_callback_user_command     = i_user_command
        it_fieldcat                 = ti_fieldcat[]
        is_layout                   = te_layout
        it_sort                     = ti_sort[]
        i_default                   = 'X'
        i_save                      = 'A'
        is_variant                  = te_variante
        is_print                    = te_print
        i_grid_title                = i_title
        i_screen_start_column       = i_start_col
        i_screen_start_line         = i_start_row
        i_screen_end_column         = i_end_col
        i_screen_end_line           = i_end_row
      TABLES
        t_outtab                    = i_table
      EXCEPTIONS
        program_error               = 1
        OTHERS                      = 2.

  ENDMETHOD.                    "display_report


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->F4_VARIANTS
* +-------------------------------------------------------------------------------------------------+
* | [<-->] P_VARIANT                      TYPE        SLIS_VARI
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD f4_variants.

    DATA:
      vl_variant_exit TYPE c LENGTH 001 VALUE space,
      vl_variant      TYPE disvariant,
      vl_def_variant  TYPE disvariant.

    CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
      EXPORTING
        is_variant    = vl_variant
        i_save        = 'A'
      IMPORTING
        e_exit        = vl_variant_exit
        es_variant    = vl_def_variant
      EXCEPTIONS
        not_found     = 1
        program_error = 2
        OTHERS        = 3.
    IF sy-subrc IS INITIAL.

      IF vl_variant_exit = space.
        p_variant = vl_def_variant-variant.
      ENDIF.

    ELSE.

      CASE sy-subrc.
        WHEN 1.
          MESSAGE s208(00)
             WITH 'Não ha variantes salvas para este programa.'
             DISPLAY LIKE 'W'.
        WHEN 2.
          MESSAGE s208(00)
             WITH 'Erro ao tentar localizar as variantes.'
             DISPLAY LIKE 'E'.
        WHEN OTHERS.
          MESSAGE s208(00)
             WITH 'Verifique se existem variantes para este programa.'
             DISPLAY LIKE 'W'.
      ENDCASE.

    ENDIF.

  ENDMETHOD.                    "f4_variants


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->FREEZE
* +-------------------------------------------------------------------------------------------------+
* | [--->] ROW                            TYPE        I
* | [--->] COL                            TYPE        I
* | [<---] SUBRC                          TYPE        SUBRC
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD freeze.

    DATA cel TYPE string.

    READ TABLE t_cols INTO vg_cols INDEX col.
    IF ( sy-subrc <> 0 ).
      subrc = 4.
    ENDIF.
    subrc = 0.

    FREE cel.
    cel = row.
    CONCATENATE cel vg_cols-posic INTO cel.
    CONDENSE cel.

* Obtem a planilha ativa
    GET PROPERTY OF vg_excel 'ActiveWindow' = vg_sheets.

* Seleciona a linha para fixar
    CALL METHOD OF vg_excel 'RANGE' = vg_cel
      EXPORTING
      #1 = cel.

    CALL METHOD OF vg_cel 'Select'.

* Congela (fixa) o cabeçalho (exibição)
    SET PROPERTY OF vg_sheets 'FreezePanes' = 1.

  ENDMETHOD.                    "freeze


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->MOVE_PARA_TABELA
* +-------------------------------------------------------------------------------------------------+
* | [--->] SEPARADOR                      TYPE        C (default =';')
* | [--->] USA_CONVERSOR                  TYPE        C (default =SPACE)
* | [<-->] DADOS                          TYPE        TABLE
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD move_para_tabela.

*>>> Declaração de Variavei, Tab.Interna e outros Objetos Locais
    DATA:
      vl_from        TYPE string,
      vl_to          TYPE string,
      vl_tabix       TYPE sy-tabix,
      vl_campos      TYPE string,
      vl_type        TYPE c LENGTH 001,
      vl_names       TYPE sydes_nameinfo,
      tl_table       TYPE sydes_desc.

    FIELD-SYMBOLS:
      <t_dados>      TYPE ANY,
      <vl_from>      TYPE string,
      <vl_to>        TYPE ANY.

*>>> Lista os campos da tabela interna do parâmetro de entrada
    CLEAR: t_tabela[].
    FREE tl_table.
    DESCRIBE FIELD dados INTO tl_table.
    IF NOT ( tl_table IS INITIAL ).

      DELETE tl_table-names WHERE name+0(2) = '=='.
      DELETE ADJACENT DUPLICATES FROM tl_table-names
                            COMPARING name.
      DELETE tl_table-names FROM 1 TO 2.

      LOOP AT tl_table-names INTO vl_names.

        SEARCH vl_names-name FOR '-' IN CHARACTER MODE.
        CHECK ( sy-subrc <> 0 ).

        APPEND vl_names-name TO t_tabela.

      ENDLOOP.

    ENDIF.

*>>> Inicia o processo de movimentação de dados
    CLEAR dados[].
    LOOP AT t_arquivo INTO vg_arquivo.

      APPEND INITIAL LINE TO dados ASSIGNING <t_dados>.

      CLEAR t_campos[].
      SPLIT vg_arquivo-linha AT separador INTO TABLE t_campos.

*>>> Processo dinâmico da captura da campos
      LOOP AT t_campos INTO vl_campos.

        vl_tabix = sy-tabix.

        CLEAR vl_from.
        vl_from = vl_campos.

        READ TABLE t_tabela INTO vg_tabela INDEX vl_tabix.
        CHECK sy-subrc = 0.

        CLEAR vl_to.
        CONCATENATE '<T_DADOS>-' vg_tabela-campo INTO vl_to.
        CONDENSE vl_to NO-GAPS.

        ASSIGN (vl_to) TO <vl_to>.
        CHECK ( <vl_to> IS ASSIGNED ).

        IF ( usa_conversor <> space ).
          CLEAR vl_type.
          DESCRIBE FIELD <vl_to> TYPE vl_type.
          CASE vl_type.
            WHEN 'P'.  "... Valor com Decimais
              TRANSLATE vl_from USING '. '.
              TRANSLATE vl_from USING ',.'.
              CONDENSE vl_from NO-GAPS.
            WHEN 'D'.  "... Data
              TRANSLATE vl_from USING '/.'.
              TRANSLATE vl_from USING '-.'.
          ENDCASE.
        ENDIF.

        <vl_to> = vl_from.

        UNASSIGN <vl_to>.

      ENDLOOP.  "<<< t_tabela[]

    ENDLOOP.  "<<< ti_arquivo[]

  ENDMETHOD.                    "move_para_tabela


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->OPEN_EXCEL
* +-------------------------------------------------------------------------------------------------+
* | [--->] NAME                           TYPE        ANY
* | [--->] DIRETORIO                      TYPE        STRING
* | [<---] SUBRC                          TYPE        SUBRC
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD open_excel.

*** Inicializando Excel
    CREATE OBJECT vg_excel 'EXCEL.APPLICATION'.
    IF NOT sy-subrc IS INITIAL.
      subrc = 4.
      EXIT.       "Não pôde inicializar o MS Excel.
    ENDIF.
    subrc = 0.

*** Oculta, para economizar tempo e memória
    SET PROPERTY OF vg_excel  'Visible' = 0.

*** Desabilita os alertas do excel
    SET PROPERTY OF vg_excel 'DisplayAlerts' = 0.

*** Define a pasta de trabalho atual.
    CALL METHOD OF vg_excel 'Workbooks' = vg_workbook.

*** Adiciona a pasta (workbook)
    CALL METHOD OF vg_workbook 'Add' = vg_plan
      EXPORTING
      #1 = 1.

*** Nomeia a planilha
    CALL METHOD OF vg_excel 'Sheets' = vg_sheets
      EXPORTING
      #1 = 1.
    SET PROPERTY OF vg_sheets 'Name' = name.

  ENDMETHOD.                    "OPEN_EXCEL


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->PROCESSANDO
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD processando.

    DATA:
      vl_hora        TYPE c LENGTH 008 VALUE space,
      vl_texto       TYPE string.

    GET TIME.
    WRITE sy-uzeit TO vl_hora.

    CONCATENATE vl_hora texto 'Processando...' INTO vl_texto SEPARATED BY space.

    CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
      EXPORTING
        text = vl_texto.

  ENDMETHOD.                    "processando


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->RESET_ALV
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD reset_alv.

    FREE    ti_fieldcat.
    REFRESH ti_fieldcat.
    CLEAR   ti_fieldcat.

    FREE    ti_listheader.
    REFRESH ti_listheader.
    CLEAR   ti_listheader.

    FREE    ti_sort.
    REFRESH ti_sort.
    CLEAR   ti_sort.

    FREE    ti_linecolor.
    REFRESH ti_linecolor.
    CLEAR   ti_linecolor.

  ENDMETHOD.                    "reset_alv


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->SALVA_ARQUIVO
* +-------------------------------------------------------------------------------------------------+
* | [--->] DADOS                          TYPE        TABLE
* | [--->] SEPARADOR                      TYPE        STRING (default =';')
* | [--->] USA_CONVERSOR                  TYPE        C (default =SPACE)
* | [--->] DIRETORIO                      TYPE        STRING
* | [<---] SUBRC                          TYPE        SUBRC
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD salva_arquivo.

*>>> Declaração de Variavei, Tab.Interna e outros Objetos Locais
    TYPES:
      BEGIN OF ty_arquivo,
        linha        TYPE string,
      END   OF ty_arquivo.

    DATA:
      vl_linha       TYPE string,
      vl_to          TYPE string,
      vl_tabix       TYPE sy-tabix,
      vl_campo       TYPE string,
      vl_string      TYPE c LENGTH 2000  VALUE space,
      vl_filetype    TYPE c LENGTH 010   VALUE 'ASC',
      vl_separator   TYPE c LENGTH 001   VALUE space,
      vl_len         TYPE n LENGTH 006,
      vl_type        TYPE c LENGTH 001,
      vl_data        TYPE c LENGTH 010,
      vl_names       TYPE sydes_nameinfo,
      tl_table       TYPE sydes_desc,
      tl_arquivo     TYPE TABLE OF ty_arquivo.

    FIELD-SYMBOLS:
      <tl_arquivo>   TYPE ty_arquivo,
      <t_dados>      TYPE ANY,
      <vl_to>        TYPE ANY.

*>>> Lista os campos da tabela interna do parâmetro de entrada
    CLEAR: t_tabela[].
    FREE tl_table.
    DESCRIBE FIELD dados INTO tl_table.
    IF NOT ( tl_table IS INITIAL ).

      DELETE tl_table-names WHERE name+0(2) = '=='.
      DELETE ADJACENT DUPLICATES FROM tl_table-names
                            COMPARING name.
      DELETE tl_table-names FROM 1 TO 2.

      LOOP AT tl_table-names INTO vl_names.

        SEARCH vl_names-name FOR '-' IN CHARACTER MODE.
        CHECK ( sy-subrc <> 0 ).

        APPEND vl_names-name TO t_tabela.

      ENDLOOP.

    ENDIF.

*>>> Inicia o processo de movimentação de dados
    LOOP AT dados ASSIGNING <t_dados>.

*>>> Processo dinâmico da captura da campos
      CLEAR vl_linha.
      FREE  vl_linha.
      LOOP AT t_tabela INTO vg_tabela.

        vl_tabix = sy-tabix.

        CLEAR vl_to.
        CONCATENATE '<T_DADOS>-' vg_tabela-campo INTO vl_to.
        CONDENSE vl_to NO-GAPS.

        ASSIGN (vl_to) TO <vl_to>.
        CHECK ( <vl_to> IS ASSIGNED ).

        FREE vl_campo.
        vl_campo = <vl_to>.

        DESCRIBE FIELD <vl_to> OUTPUT-LENGTH vl_len.

        IF ( usa_conversor <> space ).
          CLEAR vl_type.
          DESCRIBE FIELD <vl_to> TYPE vl_type.
          CASE vl_type.
            WHEN 'P'.  "... Valor com Decimais
              TRANSLATE vl_campo USING '.,'.
              CONDENSE vl_campo NO-GAPS.
            WHEN 'D'.  "... Data
              CLEAR vl_data.
              WRITE <vl_to> TO vl_data USING EDIT MASK '__/__/____'.
              CLEAR vl_campo. FREE vl_campo.
              vl_campo = vl_data.
          ENDCASE.
        ENDIF.

        UNASSIGN <vl_to>.

        IF ( vl_tabix = 1 ).
          IF ( separador <> ';' ).
            WRITE vl_campo TO vl_string+0(vl_len).
            vl_linha = vl_string+0(vl_len).
          ELSE.
            vl_linha = vl_campo.
          ENDIF.
        ELSE.
          IF ( separador <> ';' ).
            WRITE vl_campo TO vl_string+0(vl_len).
            CONCATENATE vl_linha separador vl_string+0(vl_len) INTO vl_linha.
          ELSE.
            CONCATENATE vl_linha separador vl_campo INTO vl_linha.
          ENDIF.
        ENDIF.

      ENDLOOP.  "<<< t_tabela[]

      CHECK NOT ( vl_linha IS INITIAL ).

      APPEND INITIAL LINE TO tl_arquivo ASSIGNING <tl_arquivo>.
      <tl_arquivo>-linha = vl_linha.

    ENDLOOP.  "<<< dados[]

    CASE separador.
      WHEN ';'.      vl_filetype = 'ASC'. vl_separator = space.
      WHEN 'EXCEL'.  vl_filetype = 'WK1'. vl_separator = 'X'.
    ENDCASE.

    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename                = diretorio
        filetype                = vl_filetype
        write_field_separator   = vl_separator
      TABLES
        data_tab                = tl_arquivo[]
      EXCEPTIONS
        file_write_error        = 1
        no_batch                = 2
        gui_refuse_filetransfer = 3
        invalid_type            = 4
        no_authority            = 5
        unknown_error           = 6
        header_not_allowed      = 7
        separator_not_allowed   = 8
        filesize_not_allowed    = 9
        header_too_long         = 10
        dp_error_create         = 11
        dp_error_send           = 12
        dp_error_write          = 13
        unknown_dp_error        = 14
        access_denied           = 15
        dp_out_of_memory        = 16
        disk_full               = 17
        dp_timeout              = 18
        file_not_found          = 19
        dataprovider_exception  = 20
        control_flush_error     = 21
        OTHERS                  = 22.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid
            TYPE 'S'
          NUMBER sy-msgno
            WITH sy-msgv1
                 sy-msgv2
                 sy-msgv3
                 sy-msgv4 DISPLAY LIKE sy-msgty.
      subrc = 4.
      EXIT.
    ENDIF.

    subrc = 0.

  ENDMETHOD.                    "salva_arquivo


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->SALVA_EM_CSV
* +-------------------------------------------------------------------------------------------------+
* | [--->] DADOS                          TYPE        TABLE
* | [--->] DIRETORIO                      TYPE        STRING
* | [<---] SUBRC                          TYPE        SUBRC
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD salva_em_csv.

    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename                = diretorio
        filetype                = 'ASC'
      TABLES
        data_tab                = dados[]
      EXCEPTIONS
        file_write_error        = 1
        no_batch                = 2
        gui_refuse_filetransfer = 3
        invalid_type            = 4
        no_authority            = 5
        unknown_error           = 6
        header_not_allowed      = 7
        separator_not_allowed   = 8
        filesize_not_allowed    = 9
        header_too_long         = 10
        dp_error_create         = 11
        dp_error_send           = 12
        dp_error_write          = 13
        unknown_dp_error        = 14
        access_denied           = 15
        dp_out_of_memory        = 16
        disk_full               = 17
        dp_timeout              = 18
        file_not_found          = 19
        dataprovider_exception  = 20
        control_flush_error     = 21
        OTHERS                  = 22.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid
            TYPE 'S'
          NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
         DISPLAY LIKE sy-msgty.
      subrc = 4.
    ELSE.
      subrc = 0.
    ENDIF.

  ENDMETHOD.                    "salva_em_csv


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->SALVA_EM_EXCEL
* +-------------------------------------------------------------------------------------------------+
* | [--->] NAME                           TYPE        ANY (default ='PLAN_DADOS')
* | [--->] DADOS                          TYPE        TABLE
* | [--->] USA_CONVERSOR                  TYPE        C (default =SPACE)
* | [--->] DIRETORIO                      TYPE        STRING
* | [<---] SUBRC                          TYPE        SUBRC
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD salva_em_excel.

*>>> Declaração de Variavei, Tab.Interna e outros Objetos Locais
    TYPES:
      BEGIN OF ty_arquivo,
        linha        TYPE string,
      END   OF ty_arquivo.

    DATA:
      vl_linha       TYPE string,
      vl_to          TYPE string,
      vl_tabix1      TYPE sy-tabix,
      vl_tabix2      TYPE sy-tabix,
      vl_campo       TYPE string,
      vl_string      TYPE c LENGTH 2000  VALUE space,
      vl_filetype    TYPE c LENGTH 010   VALUE 'ASC',
      vl_separator   TYPE c LENGTH 001   VALUE space,
      vl_len         TYPE n LENGTH 006,
      vl_type        TYPE c LENGTH 001,
      vl_data        TYPE c LENGTH 010,
      vl_names       TYPE sydes_nameinfo,
      tl_table       TYPE sydes_desc,
      tl_arquivo     TYPE TABLE OF ty_arquivo,
      vl_subrc       TYPE subrc.

    FIELD-SYMBOLS:
      <tl_arquivo>   TYPE ty_arquivo,
      <t_dados>      TYPE ANY,
      <vl_to>        TYPE ANY.

*>>> Lista os campos da tabela interna do parâmetro de entrada
    CLEAR: t_tabela[].
    FREE tl_table.
    DESCRIBE FIELD dados INTO tl_table.
    IF NOT ( tl_table IS INITIAL ).

      DELETE tl_table-names WHERE name+0(2) = '=='.
      DELETE ADJACENT DUPLICATES FROM tl_table-names
                            COMPARING name.
      DELETE tl_table-names FROM 1 TO 2.

      LOOP AT tl_table-names INTO vl_names.

        me->processando( ).

        SEARCH vl_names-name FOR '-' IN CHARACTER MODE.
        CHECK ( sy-subrc <> 0 ).

        APPEND vl_names-name TO t_tabela.

      ENDLOOP.

    ENDIF.

    CALL METHOD me->open_excel
      EXPORTING
        name      = name
        diretorio = diretorio
      IMPORTING
        subrc     = vl_subrc.

*>>> Inicia o processo de movimentação de dados
    LOOP AT dados ASSIGNING <t_dados>.

      me->processando( ).

      vl_tabix1 = sy-tabix.

*>>> Processo dinâmico da captura da campos
      CLEAR vl_tabix2.
      LOOP AT t_tabela INTO vg_tabela.

        me->processando( ).

        CLEAR vl_to.
        CONCATENATE '<T_DADOS>-' vg_tabela-campo INTO vl_to.
        CONDENSE vl_to NO-GAPS.

        ASSIGN (vl_to) TO <vl_to>.
        CHECK ( <vl_to> IS ASSIGNED ).

        ADD 1 TO vl_tabix2.

        FREE vl_campo.
        vl_campo = <vl_to>.

        IF ( usa_conversor <> space ).
          CLEAR vl_type.
          DESCRIBE FIELD <vl_to> TYPE vl_type.
          CASE vl_type.
            WHEN 'P'.  "... Valor com Decimais
              TRANSLATE vl_campo USING '.,'.
              CONDENSE vl_campo NO-GAPS.
            WHEN 'D'.  "... Data
              CLEAR vl_data.
              WRITE <vl_to> TO vl_data USING EDIT MASK '__/__/____'.
              CLEAR vl_campo. FREE vl_campo.
              vl_campo = vl_data.
          ENDCASE.
        ENDIF.

        UNASSIGN <vl_to>.

        CALL METHOD me->write_cell
          EXPORTING
            format = 'DATA'
            row    = vl_tabix1
            col    = vl_tabix2
            value  = vl_campo
          IMPORTING
            subrc  = vl_subrc.

      ENDLOOP.  "<<< t_tabela[]

    ENDLOOP.  "<<< dados[]

    CALL METHOD me->close_excel
      EXPORTING
        diretorio = diretorio.

    subrc = 0.

  ENDMETHOD.                    "salva_arquivo


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->SET_FONT
* +-------------------------------------------------------------------------------------------------+
* | [--->] CODE                           TYPE        ANY (default ='DATA')
* | [--->] NAME                           TYPE        ANY (default ='Arial')
* | [--->] SIZE                           TYPE        ANY (default =10)
* | [--->] BOLD                           TYPE        ANY (default =' ')
* | [--->] ITALIC                         TYPE        ANY (default =' ')
* | [--->] COLOR                          TYPE        ANY (default =0)
* | [--->] BGCOLOR                        TYPE        ANY (default =-4142)
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD set_font.

    FIELD-SYMBOLS: <t_fonts> TYPE ty_font.

*** Busca na tabela interna se o código já existe.
    READ TABLE t_fonts ASSIGNING <t_fonts> WITH KEY code = code.
    IF NOT sy-subrc IS INITIAL.

* Insere um novo registro.
*** Seta os valores

      APPEND INITIAL LINE TO t_fonts ASSIGNING <t_fonts>.
      <t_fonts>-name = name.
      <t_fonts>-size = size.
      IF bold IS INITIAL.
        <t_fonts>-bold = c_blank.
      ELSE.
        <t_fonts>-bold = c_x.
      ENDIF.
      IF italic IS INITIAL.
        <t_fonts>-italic = c_blank.
      ELSE.
        <t_fonts>-italic = c_x.
      ENDIF.
      <t_fonts>-color = color.
      <t_fonts>-bgcolor = bgcolor.
      <t_fonts>-code = code.

    ELSE.

* Altera o registro atual.
*** Seta os valores

      <t_fonts>-name = name.
      <t_fonts>-size = size.
      IF bold IS INITIAL.
        <t_fonts>-bold = c_blank.
      ELSE.
        <t_fonts>-bold = c_x.
      ENDIF.
      IF italic IS INITIAL.
        <t_fonts>-italic = c_blank.
      ELSE.
        <t_fonts>-italic = c_x.
      ENDIF.
      <t_fonts>-color = color.
      <t_fonts>-bgcolor = bgcolor.

    ENDIF.

  ENDMETHOD.                    "SET_FONT


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->USE_FONT
* +-------------------------------------------------------------------------------------------------+
* | [--->] FORMAT                         TYPE        ANY
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD use_font.

*** Validações
    READ TABLE t_fonts INTO vg_fonts WITH KEY code = format.

    IF NOT sy-subrc IS INITIAL.
*** Não localizou o tipo de fonte, utiliza a primeira
      READ TABLE t_fonts INTO vg_fonts INDEX 1.
    ENDIF.
    GET PROPERTY OF vg_cel 'Font' = vg_font.

*** Define a fonte (Letra)
    SET PROPERTY OF vg_font 'name' = vg_fonts-name.

*** Define o tamanho da fonte
    SET PROPERTY OF vg_font 'Size' = vg_fonts-size.

*** Define se a fonte será em italico ou não.
    IF vg_fonts-italic EQ c_x.
      SET PROPERTY OF vg_font 'Italic' = 1.
    ELSE.
      SET PROPERTY OF vg_font 'Italic' = 0.
    ENDIF.

*** Define se a fonte será em negrito ou não.
    IF vg_fonts-bold EQ c_x.
      SET PROPERTY OF vg_font 'Bold' = 1.
    ELSE.
      SET PROPERTY OF vg_font 'Bold' = 0.
    ENDIF.

*** Define a cor da letra
    SET PROPERTY OF vg_font 'ColorIndex' = vg_fonts-color.

*** Define a cor de fundo
    GET PROPERTY OF vg_cel 'Interior' = vg_interior.
    SET PROPERTY OF vg_interior 'ColorIndex' = vg_fonts-bgcolor.
    SET PROPERTY OF vg_interior 'Pattern' = 1.

*** Libera os objetos criados
    FREE OBJECT vg_interior.
    FREE OBJECT vg_font.

  ENDMETHOD.                    "use_font


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method CL_ABAP->WRITE_CELL
* +-------------------------------------------------------------------------------------------------+
* | [--->] FORMAT                         TYPE        ANY (default ='DATA')
* | [--->] ROW                            TYPE        ANY
* | [--->] COL                            TYPE        ANY
* | [--->] VALUE                          TYPE        ANY(optional)
* | [<---] SUBRC                          TYPE        SUBRC
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD write_cell.

*** Valida se já foi definido algum tipo de fonte
    IF t_fonts[] IS INITIAL.
      subrc = 4.
      EXIT.    "Tabela de fontes vazia.
    ENDIF.
    subrc = 0.

    READ TABLE t_cols INTO vg_cols INDEX row.
    IF ( sy-subrc <> 0 ).
      subrc = 4.
      EXIT.    "Tabela de fontes vazia.
    ENDIF.
    subrc = 0.

*** Cria o objeto 'Célula' do Excel
    CALL METHOD OF vg_excel 'Cells' = vg_cel
      EXPORTING
      #1 = row
      #2 = col.

*** Grava o valor
    SET PROPERTY OF vg_cel 'Value' = value.

*** Define o formato da célula
    CALL METHOD me->use_font
      EXPORTING
        format = format.

*** Libera os objetos criados
    FREE OBJECT vg_cel.

  ENDMETHOD.                    "write_cell

ENDCLASS.                    "CL_ABAP IMPLEMENTATION
*______________________________________________________________________*
*   Declaração do objecto que será utilizado no desenvolvimento
*¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯*
DATA obj_abap      TYPE REF TO cl_abap.

***[ End of INCLUDE ]***************************************************

Arquivos para Download:

ZGERA_XML.txt

Queremos saber sua opinião