Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Internal table into excel and sending the same as attachment through mail

Former Member
0 Kudos

Hi all,

I have a requirement of converting Internal table into excel and sending the same as attachment through mail.

Thank you.

1 ACCEPTED SOLUTION

Former Member

" data declaration
" variable declaration
data :  v_lines_bin    type i,              "no of lines for excel data
        message_lines  type i,              "no of lines for body of mail
        v_mailaddr     type  adr6-smtp_addr. " storing email id

" class declaration
CLASS: cl_abap_char_utilities DEFINITION LOAD.

" internal table declaration
data: it_objpack  type standard table of  sopcklsti1,
      imessage    type standard table of  solisti1 ,
      it_reclist  type standard table of  somlreci1 ,
      it_objbin  type standard table of  solisti1 ,
      c_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
      c_ret TYPE c VALUE cl_abap_char_utilities=>cr_lf,

"work area declaration
      wa_objbin LIKE LINE OF it_objbin,
      wa_imessage LIKE LINE OF imessage,
      wa_it_reclist  type  somlreci1,
      w_doc_chng     type  sodocchgi1,
      wa_it_orders   TYPE sfc_poco,
      wa_it_objpack  type  sopcklsti1.


" populate the text for body of the mail
clear wa_imessage.
wa_imessage-line = text-004.                     " Please find above the excel attached for the list of unproccesed orders
append wa_imessage to imessage.



describe table imessage lines message_lines.   "no of lines for body of mail

READ TABLE imessage INTO wa_imessage index message_lines.

"document information
    w_doc_chng-obj_name = text-005.            " Excel
    w_doc_chng-obj_descr = text-006.           "Excel For Unprocessed Orders
    w_doc_chng-sensitivty = text-007.          " F ->Functional object
    w_doc_chng-doc_size = ( message_lines - 1 ) * 255 + strlen( wa_imessage-line ).  " calculating total size of doc


" displaying planned order no , material , plant in the excel

    CONCATENATE 'PLANNED ORDER' 'MATERIAL' 'PLANT' into wa_objbin SEPARATED BY c_tab.
    append wa_objbin to it_objbin.

    clear : wa_objbin.
    loop at it_orders INTO wa_it_orders. "It_orders is the internal table to be transferred to excel
      CONCATENATE wa_it_orders-plnum wa_it_orders-matnr wa_it_orders-plwrk into wa_objbin SEPARATED BY c_tab.
      CONCATENATE c_ret wa_objbin INTO wa_objbin.
      APPEND wa_objbin to it_objbin.
    endloop.

describe table it_objbin LINES v_lines_bin.   " no of lines for excel data


" pack the data as RAW
clear wa_it_objpack-transf_bin.                     "Obj. to be transported not in binary form
    wa_it_objpack-head_start = 1.                   "Start line of object header in transport packet
    wa_it_objpack-head_num = 0.                     "Number of lines of an object header in object packet
    wa_it_objpack-body_start = 1.                   "Start line of object contents in an object packet
    wa_it_objpack-body_num = message_lines.         "Number of lines of the mail body
    wa_it_objpack-doc_type = text-008.           "RAW
    append wa_it_objpack to it_objpack.


" pack the data as excel
    wa_it_objpack-transf_bin = text-009.      " X
    wa_it_objpack-head_start = 1.
    wa_it_objpack-head_num = 1.
    wa_it_objpack-body_start = 1.
    wa_it_objpack-body_num = v_lines_bin.         "no of lines of it_orders to give no of unprocessed orders
    wa_it_objpack-doc_type = text-010.         " XLS ->  excel fomat
    wa_it_objpack-obj_name = text-011.         " EXCEL ATTACHMENT

" attachment name
    concatenate text-012 '.XLS' into wa_it_objpack-obj_descr.      " UNPROCESSED ORDERS
    wa_it_objpack-doc_size = v_lines_bin * 255.
    append wa_it_objpack to it_objpack.

" creating email id
CONCATENATE p_email text-015  into v_mailaddr.   "@abc.com

* e-mail receivers.
    clear wa_it_reclist.
    wa_it_reclist-receiver = v_mailaddr.
    wa_it_reclist-express =  text-009.                      " X
    wa_it_reclist-rec_type = text-013.                      " U ->  Internet address
    append wa_it_reclist to it_reclist.


" sending mail
   CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
     EXPORTING
       document_data                    = w_doc_chng
      PUT_IN_OUTBOX                    = 'X'
      COMMIT_WORK                      = 'X'
*    IMPORTING
*      SENT_TO_ALL                      =
*      NEW_OBJECT_ID                    =
     tables
       packing_list                     = it_objpack
*      OBJECT_HEADER                    =
      CONTENTS_BIN                      = it_objbin
      CONTENTS_TXT                      = imessage
*      CONTENTS_HEX                     =
*      OBJECT_PARA                      =
*      OBJECT_PARB                      =
       receivers                        = it_reclist
    EXCEPTIONS
      TOO_MANY_RECEIVERS               = 1
      DOCUMENT_NOT_SENT                = 2
      DOCUMENT_TYPE_NOT_EXIST          = 3
      OPERATION_NO_AUTHORIZATION       = 4
      PARAMETER_ERROR                  = 5
      X_ERROR                          = 6
      ENQUEUE_ERROR                    = 7
      OTHERS                           = 8
             .

   commit work.

   IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
   ENDIF.
4 REPLIES 4

Former Member

" data declaration
" variable declaration
data :  v_lines_bin    type i,              "no of lines for excel data
        message_lines  type i,              "no of lines for body of mail
        v_mailaddr     type  adr6-smtp_addr. " storing email id

" class declaration
CLASS: cl_abap_char_utilities DEFINITION LOAD.

" internal table declaration
data: it_objpack  type standard table of  sopcklsti1,
      imessage    type standard table of  solisti1 ,
      it_reclist  type standard table of  somlreci1 ,
      it_objbin  type standard table of  solisti1 ,
      c_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
      c_ret TYPE c VALUE cl_abap_char_utilities=>cr_lf,

"work area declaration
      wa_objbin LIKE LINE OF it_objbin,
      wa_imessage LIKE LINE OF imessage,
      wa_it_reclist  type  somlreci1,
      w_doc_chng     type  sodocchgi1,
      wa_it_orders   TYPE sfc_poco,
      wa_it_objpack  type  sopcklsti1.


" populate the text for body of the mail
clear wa_imessage.
wa_imessage-line = text-004.                     " Please find above the excel attached for the list of unproccesed orders
append wa_imessage to imessage.



describe table imessage lines message_lines.   "no of lines for body of mail

READ TABLE imessage INTO wa_imessage index message_lines.

"document information
    w_doc_chng-obj_name = text-005.            " Excel
    w_doc_chng-obj_descr = text-006.           "Excel For Unprocessed Orders
    w_doc_chng-sensitivty = text-007.          " F ->Functional object
    w_doc_chng-doc_size = ( message_lines - 1 ) * 255 + strlen( wa_imessage-line ).  " calculating total size of doc


" displaying planned order no , material , plant in the excel

    CONCATENATE 'PLANNED ORDER' 'MATERIAL' 'PLANT' into wa_objbin SEPARATED BY c_tab.
    append wa_objbin to it_objbin.

    clear : wa_objbin.
    loop at it_orders INTO wa_it_orders. "It_orders is the internal table to be transferred to excel
      CONCATENATE wa_it_orders-plnum wa_it_orders-matnr wa_it_orders-plwrk into wa_objbin SEPARATED BY c_tab.
      CONCATENATE c_ret wa_objbin INTO wa_objbin.
      APPEND wa_objbin to it_objbin.
    endloop.

describe table it_objbin LINES v_lines_bin.   " no of lines for excel data


" pack the data as RAW
clear wa_it_objpack-transf_bin.                     "Obj. to be transported not in binary form
    wa_it_objpack-head_start = 1.                   "Start line of object header in transport packet
    wa_it_objpack-head_num = 0.                     "Number of lines of an object header in object packet
    wa_it_objpack-body_start = 1.                   "Start line of object contents in an object packet
    wa_it_objpack-body_num = message_lines.         "Number of lines of the mail body
    wa_it_objpack-doc_type = text-008.           "RAW
    append wa_it_objpack to it_objpack.


" pack the data as excel
    wa_it_objpack-transf_bin = text-009.      " X
    wa_it_objpack-head_start = 1.
    wa_it_objpack-head_num = 1.
    wa_it_objpack-body_start = 1.
    wa_it_objpack-body_num = v_lines_bin.         "no of lines of it_orders to give no of unprocessed orders
    wa_it_objpack-doc_type = text-010.         " XLS ->  excel fomat
    wa_it_objpack-obj_name = text-011.         " EXCEL ATTACHMENT

" attachment name
    concatenate text-012 '.XLS' into wa_it_objpack-obj_descr.      " UNPROCESSED ORDERS
    wa_it_objpack-doc_size = v_lines_bin * 255.
    append wa_it_objpack to it_objpack.

" creating email id
CONCATENATE p_email text-015  into v_mailaddr.   "@abc.com

* e-mail receivers.
    clear wa_it_reclist.
    wa_it_reclist-receiver = v_mailaddr.
    wa_it_reclist-express =  text-009.                      " X
    wa_it_reclist-rec_type = text-013.                      " U ->  Internet address
    append wa_it_reclist to it_reclist.


" sending mail
   CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
     EXPORTING
       document_data                    = w_doc_chng
      PUT_IN_OUTBOX                    = 'X'
      COMMIT_WORK                      = 'X'
*    IMPORTING
*      SENT_TO_ALL                      =
*      NEW_OBJECT_ID                    =
     tables
       packing_list                     = it_objpack
*      OBJECT_HEADER                    =
      CONTENTS_BIN                      = it_objbin
      CONTENTS_TXT                      = imessage
*      CONTENTS_HEX                     =
*      OBJECT_PARA                      =
*      OBJECT_PARB                      =
       receivers                        = it_reclist
    EXCEPTIONS
      TOO_MANY_RECEIVERS               = 1
      DOCUMENT_NOT_SENT                = 2
      DOCUMENT_TYPE_NOT_EXIST          = 3
      OPERATION_NO_AUTHORIZATION       = 4
      PARAMETER_ERROR                  = 5
      X_ERROR                          = 6
      ENQUEUE_ERROR                    = 7
      OTHERS                           = 8
             .

   commit work.

   IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
   ENDIF.

Former Member
0 Kudos

Hi Sanu,

Try the Function Module - SAP_CONVERT_TO_XLS_FORMAT to download internal table to excel file. Try this saple code for the same -


DATA : t_itab LIKE TABLE OF spfli.
SELECT * FROM spfli INTO TABLE t_itab.
CALL FUNCTION 'SAP_CONVERT_TO_XLS_FORMAT'
  EXPORTING
*   I_FIELD_SEPERATOR          =
*   I_LINE_HEADER              =
   i_filename                 = 'c:\kk1\pin.xls'
*   I_APPL_KEEP                = ' '
  TABLES
    i_tab_sap_data             = t_itab
* CHANGING
*   I_TAB_CONVERTED_DATA       =
 EXCEPTIONS
   conversion_failed          = 1
   OTHERS                     = 2
          .
IF sy-subrc <> 0.
  WRITE : 'Error during file download'.
ENDIF.

Regards

Pinaki

Former Member
0 Kudos

Hi,

A very good link of wiki found on the forum

https://www.sdn.sap.com/irj/scn/wiki?path=/display/abap/tosend2inttablesdataastwoattachmentstomailidoutsidesapsystem

Hope this might helpful.

Pooja