There are too many articles that talk about excel attachments, no matter whether ABAP2XLS or using cl_salv_table->to_xml, etc. Sometimes no need to use a gun to shoot a mosquito when the gun & bullet are not ready.
Here is just one small code to quickly send one internal table as an excel file, which will be used as a template for myself. I would like to hide this blog if here provides this functionality. Please ignore this and sorry if it causes duplicated subjects& content…
*&---------------------------------------------------------------------*
*& Form send_email_excel
*&---------------------------------------------------------------------*
FORM send_email_excel.
CONSTANTS: lc_tab TYPE c VALUE cl_bcs_convert=>gc_tab,
lc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf.
DATA: ld_string TYPE string,
size TYPE so_obj_len,
lo_send_request TYPE REF TO cl_bcs,
lo_document TYPE REF TO cl_document_bcs,
lo_sender TYPE REF TO if_sender_bcs,
lo_recipient TYPE REF TO if_recipient_bcs VALUE IS INITIAL,
lv_file_subject TYPE so_obj_des,
lr_bcs_exception TYPE REF TO cx_bcs,
lv_subject TYPE char50,
lx_document_bcs TYPE REF TO cx_document_bcs,
lv_sent_to_all TYPE os_boolean,
lt_message_body TYPE bcsy_text,
wa_message_body LIKE LINE OF lt_message_body,
binary_content TYPE solix_tab.
DATA: lt_fields TYPE STANDARD TABLE OF dfies,
ls_fields TYPE dfies.
check s_smtp[] is not INITIAL.
*---------------------------------------
*Prepare Excel file add as attachment
*---Header Line
*---------------------------------------
CLEAR ld_string.
* Get header descriptions by 'DDIF_FIELDINFO_GET'
CALL FUNCTION 'DDIF_FIELDINFO_GET'
EXPORTING
tabname = lv_tabname
langu = sy-langu
TABLES
dfies_tab = lt_fields
EXCEPTIONS
not_found = 1
internal_error = 2
OTHERS = 3.
* or hardcode if descriptions not existed yet
CONCATENATE 'Customer' lc_tab
'Delivery' lc_tab
...
Characteristic description' lc_crlf
INTO ld_string.
*---------------------------------------
*Prepare Excel file add as attachment
*---Item Line
*---------------------------------------
LOOP AT gt_out INTO gw_out.
WRITE gw_out-wadat_ist TO v_wadat.
CONCATENATE ld_string
gw_out-con_name lc_tab
...
gw_out-con_so_mail lc_crlf
INTO ld_string.
ENDLOOP.
*----------------------------
*Convert string to Excel file
*----------------------------
TRY.
cl_bcs_convert=>string_to_solix(
EXPORTING
iv_string = ld_string
iv_codepage = '4103' "suitable for MS Excel, leave empty
iv_add_bom = 'X' "for other doc types
IMPORTING
et_solix = binary_content
ev_size = size ).
CATCH cx_bcs.
MESSAGE e445(so).
ENDTRY.
*Email subject
lv_subject = 'Email Subject here'.
*Email Body
REFRESH lt_message_body.
wa_message_body = 'Email body contents here'.
APPEND wa_message_body TO lt_message_body.
"create send request
TRY.
lo_send_request = cl_bcs=>create_persistent( ).
CATCH cx_send_req_bcs.
ENDTRY.
"put your text into the document
lv_file_subject = lv_subject.
TRY.
lo_document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = lt_message_body
i_subject = lv_subject ).
CATCH cx_document_bcs INTO lx_document_bcs.
ENDTRY.
* attached Document name
lv_subject = 'Attached File name'
TRY.
lo_document->add_attachment(
EXPORTING
i_attachment_type = 'XLS'
i_attachment_subject = lv_subject
i_attachment_size = size
i_att_content_hex = binary_content ).
CATCH cx_document_bcs INTO lx_document_bcs.
ENDTRY.
TRY.
* Add attachment
* Pass the document to send request
lo_send_request->set_document( lo_document ).
"Create sender
TRY.
lo_sender = cl_sapuser_bcs=>create( sy-uname ).
CATCH cx_address_bcs.
ENDTRY.
"Set sender
lo_send_request->set_sender( lo_sender ).
"set receiver
IF s_smtp[] IS NOT INITIAL.
LOOP AT s_smtp.
CONDENSE s_smtp-low.
lo_recipient =
cl_cam_address_bcs=>create_internet_address( s_smtp-low ).
lo_send_request->add_recipient(
EXPORTING
i_recipient = lo_recipient
* i_copy = 'X'
).
ENDLOOP.
ENDIF.
*Set recipient
lo_send_request->add_recipient(
EXPORTING
i_recipient = lo_recipient
i_express = abap_true
).
* Send email
lo_send_request->send(
EXPORTING
i_with_error_screen = abap_true
RECEIVING
result = lv_sent_to_all ).
IF sy-subrc EQ 0.
LOOP AT s_smtp.
MESSAGE i398(00) WITH 'Email Sent to' s_smtp-low ' '
text-s10.
ENDLOOP.
ELSE.
MESSAGE i398(00) WITH 'Send Email failed.' text-s10.
ENDIF.
* Commit Work to send the email
* COMMIT WORK.
CATCH cx_bcs INTO lr_bcs_exception.
MESSAGE i865(so) WITH lr_bcs_exception->error_type.
ENDTRY.
ENDFORM.
No comments:
Post a Comment