zoukankan      html  css  js  c++  java
  • ABAP——发送Excel报表邮件

    程序运行效果:

    Tcode SOST:

    代码:

    *&---------------------------------------------------------------------*
    *& Report ZPPRTEST
    *&---------------------------------------------------------------------*
    *&
    *&---------------------------------------------------------------------*
    REPORT ZPPRTEST.
    
    TABLES: afko.
    
    DATA: BEGIN OF gt_data OCCURS 0,
      aufnr LIKE afko-aufnr,
      plnbez LIKE afko-plnbez,
      gstrp LIKE afko-gstrp,
      gltrp LIKE afko-gltrp,
    END OF gt_data.
    
    DATA: BEGIN OF gt_stock OCCURS 0,
      matnr LIKE marc-matnr,
      lgort LIKE mard-lgort,
      labst LIKE mard-labst,
    END OF gt_stock.
    
    DATA:g_string       TYPE string,
          g_string2       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 afko~aufnr afko~plnbez afko~gstrp afko~gltrp
                  INTO CORRESPONDING FIELDS OF TABLE gt_data
                  FROM afko
                  WHERE aufnr IN s_aufnr.
    *-->库存
      SELECT afko~plnbez AS matnr mard~lgort mard~labst
                  INTO CORRESPONDING FIELDS OF TABLE gt_stock
                  FROM afko
                  INNER JOIN mard ON afko~plnbez EQ mard~matnr
                  WHERE aufnr IN s_aufnr
                  AND mard~werks = '2000'.
    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 process_data.
        PERFORM process_data_stock.
        PERFORM send_mail TABLES s_m1
                 USING p_t1.
      ENDIF.
    
      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 process_data.
      DATA: l_line           TYPE string,
                l_gstrp(10),
                l_gltrp(10).
      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 gt_data.
        CLEAR: l_gstrp,l_gltrp.
    
        CONCATENATE gt_data-gstrp+0(4) '/' gt_data-gstrp+4(2)
                     '/' gt_data-gstrp+6(2) INTO l_gstrp.
    
        CONCATENATE gt_data-gltrp+0(4) '/' gt_data-gltrp+4(2)
                     '/' gt_data-gltrp+6(2) INTO l_gltrp.
    
        CONCATENATE gt_data-aufnr
                                gt_data-plnbez
                                l_gstrp
                                l_gltrp
                                INTO l_line SEPARATED BY gc_tab.
    *-->断行
      CONCATENATE g_string l_line gc_crlf INTO g_string.
      ENDLOOP.
    
    ENDFORM.
    
    FORM process_data_stock.
      DATA: l_line           TYPE string,
                l_labst(20).
      CLEAR g_string2.
      CONCATENATE '物料'
                             '仓库'
                             '库存'
                             INTO g_string2 SEPARATED BY gc_tab.
      CONCATENATE g_string2 l_line gc_crlf INTO g_string2.
      LOOP AT gt_stock.
        CLEAR: l_labst.
        WRITE gt_stock-labst TO l_labst. "数字要转换成字符
        CONCATENATE gt_stock-matnr
                               gt_stock-lgort
                               l_labst
                               INTO l_line SEPARATED BY gc_tab.
        CONCATENATE g_string2 l_line gc_crlf INTO g_string2.
      ENDLOOP.
    ENDFORM.
    
    
    FORM send_mail TABLES lt_mail_addr STRUCTURE s_m1
                    USING in_subject.
      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,
                binary_content2 TYPE solix_tab.
      DATA: size  TYPE so_obj_len,
               size2   TYPE so_obj_len.
      DATA sent_to_all    TYPE os_boolean.
      DATA: l_filename     TYPE sood-objdes,
               l_filename2     TYPE sood-objdes.
    
      CONCATENATE '生产订单' '_' sy-datum INTO l_filename.
    
      CONCATENATE '库存' '_' sy-datum INTO l_filename2.
    
    * --------------------------------------------------------------
    * 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.
          cl_bcs_convert=>string_to_solix(
            EXPORTING
              iv_string   = g_string2
              iv_codepage = '4103'  "suitable for MS Excel, leave empty
              iv_add_bom  = 'X'     "for other doc types
            IMPORTING
              et_solix  = binary_content2
              ev_size   = size2 ).
        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
    
    *     邮件内容
          APPEND 'Dear Mr and Miss:' TO main_text.
          APPEND '' TO main_text.
          APPEND '     This Email is sent by systme, Do not Return the Email.' TO main_text.
          APPEND '' TO main_text.
          APPEND 'Thanks.' TO main_text.
    
          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 ).
    *     库存
          document->add_attachment(
            i_attachment_type    = 'xls'                        "#EC NOTEXT
            i_attachment_subject = l_filename2
            i_attachment_size    = size2
            i_att_content_hex    = binary_content2 ).
    
    *     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.

    其他博主的好文——SAP压缩excel并发送mail案例

    木叶飞舞之处,火亦生生不息
  • 相关阅读:
    Java 8 forEach简单例子
    Intellij IDEA 使用
    java中的sleep()和wait()的区别
    MySQL问答整理
    java 正则例子
    如何在两个线程之间共享数据
    蜻蜓FM涉嫌诈骗投资人和广告主源代码剖析
    玩DNF开启NVIDIA独显的方法
    WSAEventSelect模型编程 详解
    签名时加密失败 --“对程序集签名时出错
  • 原文地址:https://www.cnblogs.com/StephenAmell/p/10278351.html
Copyright © 2011-2022 走看看