ABAP发送报表邮件
运行效果:
代码:
*&---------------------------------------------------------------------* *& Report ZPPRTEST6 *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT ZPPRTEST6. TABLES: AFKO. DATA: BEGIN OF GT_DATA OCCURS 0, AUFNR LIKE AFKO-AUFNR, GSTRS LIKE AFKO-GSTRS, GLTRP LIKE AFKO-GLTRP, END OF GT_DATA. DATA:g_string TYPE string, g_mail_address TYPE ad_smtpadr, g_def_sel_ord TYPE c. CONSTANTS: gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab, gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf. TYPE-POOLS : SLIS. DATA: GS_LAYOUT TYPE SLIS_LAYOUT_ALV, GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV, WA_FIELDCAT TYPE SLIS_FIELDCAT_ALV. SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-B01. SELECT-OPTIONS: S_AUFNR FOR AFKO-AUFNR. SELECTION-SCREEN END OF BLOCK B1. SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-b02. PARAMETERS: c_m1 AS CHECKBOX, p_t1 TYPE so_obj_des. SELECT-OPTIONS: s_m1 FOR g_mail_address NO INTERVALS. SELECTION-SCREEN END OF BLOCK b2. START-OF-SELECTION. PERFORM GET_DATA. PERFORM DISPLAY_DATA. END-OF-SELECTION. FORM GET_DATA. SELECT * INTO CORRESPONDING FIELDS OF TABLE GT_DATA FROM AFKO WHERE AUFNR IN S_AUFNR. ENDFORM. FORM DISPLAY_DATA. DATA: L_REPID TYPE SY-REPID, L_INCLNAME TYPE TRDIR-NAME, L_IDX LIKE SY-TABIX. CLEAR: GT_FIELDCAT[], WA_FIELDCAT. L_REPID = L_INCLNAME = SY-CPROG. GS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'. CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE' EXPORTING I_PROGRAM_NAME = L_REPID I_INTERNAL_TABNAME = 'GT_DATA' I_INCLNAME = L_INCLNAME CHANGING CT_FIELDCAT = GT_FIELDCAT EXCEPTIONS INCONSISTENT_INTERFACE = 1 PROGRAM_ERROR = 2 OTHERS = 3. LOOP AT GT_FIELDCAT INTO WA_FIELDCAT. L_IDX = SY-TABIX. CASE WA_FIELDCAT-FIELDNAME. * WHEN 'SORTL'. * WA_FIELDCAT-SELTEXT_S = WA_FIELDCAT-SELTEXT_M = * WA_FIELDCAT-SELTEXT_L = '供应商简称'. * WA_FIELDCAT-DDICTXT = 'S'. ENDCASE. CLEAR WA_FIELDCAT-KEY. MODIFY GT_FIELDCAT FROM WA_FIELDCAT INDEX L_IDX. ENDLOOP. IF c_m1 = 'X'. PERFORM CONCATENATE_DATA. PERFORM SEND_MAIL TABLES s_m1 USING p_t1 'ZPPRTEST6_List'. ENDIF. PERFORM ALV_SHOW. ENDFORM. FORM ALV_SHOW. CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' EXPORTING I_CALLBACK_PROGRAM = SY-REPID IS_LAYOUT = GS_LAYOUT IT_FIELDCAT = GT_FIELDCAT[] * I_CALLBACK_PF_STATUS_SET = 'ALV_PF_STATUS' " 触发事件调用子程序 * I_CALLBACK_USER_COMMAND = 'ALV_USER_COMMAND' " 鼠标事件操作子程序 I_SAVE = 'A' TABLES T_OUTTAB = GT_DATA EXCEPTIONS PROGRAM_ERROR = 1 OTHERS = 2. ENDFORM. FORM CONCATENATE_DATA. DATA: lt_mat LIKE gt_data OCCURS 0 WITH HEADER LINE. DATA: l_line TYPE string. CLEAR: lt_mat,lt_mat[]. lt_mat[] = gt_data[]. CLEAR g_string. *-->Header Line CONCATENATE '工单' '基本开工日' '基本完工日' INTO g_string SEPARATED BY gc_tab. *-->断行 CONCATENATE g_string l_line gc_crlf INTO g_string. *-->Data LOOP AT lt_mat. CONCATENATE lt_mat-aufnr lt_mat-gstrs lt_mat-gltrp INTO l_line SEPARATED BY gc_tab. *-->断行 CONCATENATE g_string l_line gc_crlf INTO g_string. ENDLOOP. ENDFORM. FORM SEND_MAIL TABLES lt_mail_addr STRUCTURE s_m1 USING in_subject in_filename. DATA send_request TYPE REF TO cl_bcs. DATA document TYPE REF TO cl_document_bcs. DATA recipient TYPE REF TO if_recipient_bcs. DATA bcs_exception TYPE REF TO cx_bcs. DATA main_text TYPE bcsy_text. DATA binary_content TYPE solix_tab. DATA size TYPE so_obj_len. DATA sent_to_all TYPE os_boolean. DATA l_filename TYPE sood-objdes. CONCATENATE in_filename '_' sy-datum INTO l_filename. * -------------------------------------------------------------- * convert the text string into UTF-16LE binary data including * byte-order-mark. Mircosoft Excel prefers these settings * all this is done by new class cl_bcs_convert (see note 1151257) TRY. cl_bcs_convert=>string_to_solix( EXPORTING iv_string = g_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. TRY. * -------- create persistent send request ------------------------ send_request = cl_bcs=>create_persistent( ). * -------- create and set document with attachment --------------- * create document object from internal table with text * append 'Hello world!' to main_text. "#EC NOTEXT document = cl_document_bcs=>create_document( i_type = 'RAW' i_text = main_text i_subject = in_subject ). * add the spread sheet as attachment to document object document->add_attachment( i_attachment_type = 'xls' "#EC NOTEXT i_attachment_subject = l_filename i_attachment_size = size i_att_content_hex = binary_content ). * add document object to send request send_request->set_document( document ). * --------- add recipient (e-mail address) ----------------------- LOOP AT lt_mail_addr. * create recipient object recipient = cl_cam_address_bcs=>create_internet_address( lt_mail_addr-low ). * add recipient object to send request send_request->add_recipient( recipient ). ENDLOOP. * ---------- send document --------------------------------------- sent_to_all = send_request->send( i_with_error_screen = 'X' ). COMMIT WORK. IF sent_to_all IS INITIAL. MESSAGE i500(sbcoms). ELSE. MESSAGE s022(so). ENDIF. * ------------ exception handling ---------------------------------- * replace this rudimentary exception handling with your own one !!! CATCH cx_bcs INTO bcs_exception. MESSAGE i865(so) WITH bcs_exception->error_type. ENDTRY. ENDFORM.