Itab to Excel to Send Mail

How can I send itab data to excel and then send mail this file with attachment?

DATA: objpack LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
        objhead LIKE solisti1   OCCURS 0 WITH HEADER LINE,
        objbin  LIKE solisti1   OCCURS 0 WITH HEADER LINE,
        objtxt  LIKE solisti1   OCCURS 0 WITH HEADER LINE,
        reclist LIKE somlreci1  OCCURS 0 WITH HEADER LINE.
  DATA: doc_chng           LIKE sodocchgi1,
        v_tab_lines1       LIKE sy-tabix,
        v_tab_lines        LIKE sy-tabix.
  DATA: i_get_latest LIKE solisti1 OCCURS 0 WITH HEADER LINE.
  DATA: v_up  TYPE i,
        v_nup TYPE i.

*-Header
  CONCATENATE sy-datum ' 
         INTO objbin-line.
  APPEND objbin.
  CLEAR objbin.

  CONCATENATE objbin cl_abap_char_utilities=>newline INTO objbin.
  APPEND objbin.
  CLEAR objbin.

  CONCATENATE text-t04
              cl_abap_char_utilities=>horizontal_tab
              cl_abap_char_utilities=>horizontal_tab
              cl_abap_char_utilities=>horizontal_tab
              sy-uname
         INTO objbin-line.
  CONCATENATE cl_abap_char_utilities=>cr_lf objbin-line INTO objbin-line.
  APPEND objbin.
  CLEAR objbin.

................."Continue.."
*-Body
  LOOP AT t_alv INTO wa_alv.

*-G.V.
    CONCATENATE cl_abap_char_utilities=>horizontal_tab wa_alv-vkgrp
           INTO objbin-line.
    APPEND objbin.
    CLEAR objbin.

*-Descripcion G.V.
    CONCATENATE cl_abap_char_utilities=>horizontal_tab wa_alv-bezei
                cl_abap_char_utilities=>horizontal_tab
           INTO objbin-line.
    APPEND objbin.
    CLEAR objbin.

*-Cuenta Madre
    CONCATENATE cl_abap_char_utilities=>horizontal_tab wa_alv-cuenta_madre
           INTO objbin-line.
    APPEND objbin.
    CLEAR objbin.

............ "Continue.."

ENDLOOP.

*   Appending the body of the Mail
  APPEND objtxt.
  DESCRIBE TABLE objtxt LINES v_tab_lines.
  READ TABLE objtxt INDEX v_tab_lines.
  doc_chng-doc_size = ( v_tab_lines - 1 ) * 375 + STRLEN( objtxt ).

*   Creation of the entry for the Mail Contents
  CLEAR objpack-transf_bin.
  objpack-head_start = 1.
  objpack-head_num = 0.
  objpack-body_start = 1.
  objpack-body_num = v_tab_lines.
  objpack-doc_type = 'RAW'.
  APPEND objpack.

  DESCRIBE TABLE objbin LINES v_tab_lines.
  objhead = 'Plan Crecimiento.xls'(060). " Attachment Name
  APPEND objhead.

*   Creation of the entry for the Plan Crecimiento attachment
  objpack-transf_bin = 'X'.
  objpack-head_start = 1.
  objpack-head_num = 1.
  objpack-body_start = 1.
  objpack-body_num = v_tab_lines.
  objpack-doc_type = 'XLS'.
  objpack-obj_name = 'Titulo'(059).
  objpack-obj_descr = 'Titulo'(059).
  objpack-doc_size = v_tab_lines * 375.
  APPEND objpack.

*   Completing the recipient list
  REFRESH reclist.
  reclist-receiver = lv_mail2.
  reclist-rec_type = 'U'.
  APPEND reclist.

*   Sending the document
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = doc_chng
      put_in_outbox              = 'X'
    TABLES
      packing_list               = objpack
      object_header              = objhead
      contents_bin               = objbin
      contents_txt               = objtxt
      receivers                  = reclist
    EXCEPTIONS
      too_many_receivers         = 1
      document_not_sent          = 2
      operation_no_authorization = 4
      OTHERS                     = 99.

  REFRESH reclist.

  COMMIT WORK.

  CALL FUNCTION 'SO_DEQUEUE_UPDATE_LOCKS'.

* Equivalent for the Scot transaction, that is not automatic
  SUBMIT rsconn01 AND RETURN.
  COMMIT WORK.

Read Also
RFC conver spool to a PDF document

ABAP Books List
ABAP/4 Certification, Programming, Smartforms, Sapscripts and Object Oriented Programming Books

Smart Forms
SAP Smartforms

ABAP Menu:
ABAP Example Hints and Tips

Return to Index:-
SAP ABAP/4 Programming, Basis Administration, Configuration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.  The site www.gotothings.com is in no way affiliated with SAP AG.
Any unauthorised copying or mirroring is prohibited.