• SAP压缩excel并发送mail案例


    "SAP压缩附件 

      1 REPORT ZMMR0033_DEL7 .
      2 
      3 TYPES: BEGIN OF bin_file,
      4            name TYPE string,
      5           size TYPE i,
      6           data TYPE solix_tab,
      7      END OF bin_file.
      8 
      9 DATA: lv_filename TYPE string,
     10           wa_bindata TYPE bin_file,
     11           it_bindata TYPE standard TABLE OF bin_file,
     12           oref_zip TYPE REF TO cl_abap_zip,
     13           lv_zip_xstring TYPE xstring,
     14           lv_xstring TYPE xstring,
     15           lv_xstring2 TYPE xstring,
     16           lv_path TYPE string,
     17           it_filetab TYPE filetable,
     18           ret_code TYPE i,
     19           v_usr TYPE i,
     20           v_zip_size TYPE i,
     21           it_zip_bin_data TYPE STANDARD TABLE OF raw255,
     22           v_dest_filepath TYPE string.
     23 
     24 DATA: BEGIN OF it_t001 OCCURS 0,
     25         bukrs     TYPE t001-bukrs,
     26         butxt     TYPE t001-butxt,
     27         land1 TYPE t001-LAND1,
     28         waers TYPE t001-waers,
     29   END OF it_t001.
     30 
     31   DATA main_text      TYPE bcsy_text.
     32   DATA: l_c_detal_1  TYPE solix_tab,
     33         l_detal_1     TYPE so_obj_len,
     34         l_filename_detal_1 TYPE sood-objdes.
     35 
     36 DATA: g_datel_1     TYPE string.
     37 CONSTANTS: gc_tab  TYPE c VALUE cl_bcs_convert=>gc_tab,
     38                     gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf.
     39 
     40 DATA: lv_filesize TYPE i.
     41 DATA: wa_filetab LIKE LINE OF it_filetab.
     42 DATA:  g_mail_address TYPE ad_smtpadr.
     43 
     44 SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE TEXT-b03.
     45         SELECT-OPTIONS: s_addr FOR g_mail_address NO INTERVALS DEFAULT'1164684178@qq.com'.
     46 SELECTION-SCREEN END OF BLOCK b3.
     47 
     48 START-OF-SELECTION.
     49       PERFORM get_data.
     50       PERFORM DETAIL_DATA_1.
     51       PERFORM text.
     52 *      PERFORM send_mail2 .
     53 END-OF-SELECTION.
     54 
     55 FORM get_data .
     56 
     57   SELECT * FROM t001
     58     INTO CORRESPONDING FIELDS OF TABLE it_t001 ."UP TO 10 ROWS.
     59 
     60 ENDFORM. " get_data
     61 
     62 FORM DETAIL_DATA_1 .
     63   CLEAR: g_datel_1.
     64  DATA: l_line TYPE string.
     65  DATA:lv_text1(50),
     66           lv_text2(50),
     67           lv_text3(50),
     68           lv_text4(50),
     69           lv_text5(50).
     70 *--> column header
     71   CONCATENATE '存储地点' '工厂' '物料类型'
     72               INTO l_line SEPARATED BY gc_tab.
     73 *--> 斷行
     74       CONCATENATE g_datel_1 l_line gc_crlf INTO g_datel_1.
     75     LOOP AT it_t001.
     76 
     77         lv_text1 = it_t001-bukrs .
     78         lv_text2 = it_t001-butxt .
     79         lv_text3 = it_t001-land1 .
     80         lv_text4 = it_t001-waers .
     81         lv_text5 = it_t001-bukrs .
     82 
     83 
     84     CONCATENATE lv_text1 lv_text2 lv_text3 lv_text4 lv_text5
     85                           INTO l_line SEPARATED BY gc_tab.
     86 *--> 斷行
     87           CONCATENATE g_datel_1 l_line gc_crlf INTO g_datel_1.
     88   ENDLOOP.
     89 
     90   TRY.
     91       cl_bcs_convert=>string_to_solix(
     92         EXPORTING
     93           iv_string   = g_datel_1
     94           iv_codepage = '4103'  "suitable for MS Excel, leave empty
     95           iv_add_bom  = 'X'     "for other doc types
     96         IMPORTING
     97           et_solix  = l_c_detal_1
     98           ev_size   = l_detal_1 ).
     99     CATCH cx_bcs.
    100       MESSAGE e445(so).
    101   ENDTRY.
    102 
    103 ENDFORM.
    104 
    105 FORM text. " 压缩文件
    106 
    107     DATA: l_send_request  TYPE REF TO cl_bcs,
    108         l_zip           TYPE REF TO cl_abap_zip,
    109         l_sender        TYPE REF TO cl_sapuser_bcs,
    110         l_recipient     TYPE REF TO cl_cam_address_bcs,
    111         l_body_text     TYPE bcsy_text,
    112         l_obj_len       TYPE so_obj_len,
    113         l_subject       TYPE so_obj_des,
    114         l_exception     TYPE REF TO cx_bcs,
    115         l_message       TYPE string,
    116         l_content       TYPE solix_tab,
    117         l_html_xstr     TYPE xstring,
    118         l_zip_xstr      TYPE xstring,
    119         l_len           TYPE i,
    120         l_document      TYPE REF TO cl_document_bcs.
    121 
    122  DATA: lv_codepage(4) TYPE n,
    123            l_encoding(20),
    124            xstr TYPE xstring.
    125 *           get_buffer TYPE buffer.
    126 
    127 " create object
    128   CREATE OBJECT oref_zip.
    129 
    130 
    131 DO 1 TIMES.  " 产生本地 zip 附件 及zip mail转换用
    132    wa_bindata-data = l_c_detal_1.
    133    wa_bindata-size = l_detal_1.
    134    wa_bindata-name = 'aa.xls'.
    135       APPEND wa_bindata TO it_bindata.
    136 ENDDO.
    137 
    138     "将文件换换为xstring 
    139      CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    140     EXPORTING
    141       input_length = wa_bindata-size
    142      IMPORTING
    143        buffer = lv_xstring
    144        TABLES
    145        binary_tab = wa_bindata-data. 
    146       
    147       
    148      CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
    149     EXPORTING
    150       input_length = wa_bindata-size
    151      IMPORTING
    152        buffer = lv_xstring2
    153        TABLES
    154        binary_tab = wa_bindata-data. " 可替换为其他不同data
    155      
    156     "1. 产生本端zip   
    157     "2.zip mail 可省略以下代码 
    158     oref_zip->add( name = wa_bindata-name
    159             content = lv_xstring ).
    160 
    161 *****************************本端 zip ************************************
    162 *-----> 保存附件到压缩文件夹
    163   lv_zip_xstring = oref_zip->save( ).
    164   CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
    165   EXPORTING
    166     buffer = lv_zip_xstring
    167   IMPORTING
    168       output_length = v_zip_size
    169    TABLES
    170      binary_tab = it_zip_bin_data.
    171 
    172 *****************************************************
    173 * 选择路径保存文件
    174   cl_gui_frontend_services=>file_save_dialog(
    175   EXPORTING
    176     window_title = 'SELECT THE LOCATING TO SAVE THE FILE'
    177     file_filter = '(*.zip)|*.ZIP|'
    178     CHANGING
    179       filename = lv_filename
    180       path = lv_path
    181       fullpath = v_dest_filepath ).
    182 **
    183 ** 试图替换文件
    184     cl_gui_frontend_services=>gui_download(
    185     EXPORTING
    186       bin_filesize = v_zip_size
    187       filename = v_dest_filepath
    188       filetype = 'BIN'
    189       IMPORTING
    190         filelength =  lv_filesize
    191         CHANGING
    192           data_tab  = it_zip_bin_data ).
    193 **************************************end 本端 zip *********************
    194 
    195   TRY.
    196 
    197       l_send_request = cl_bcs=>create_persistent( ).
    198       l_subject = '压缩文件名称'.
    199       APPEND:  '<HTML><BODY>'                                              TO l_body_text,
    200                  'Hi,'                                                     TO l_body_text,
    201                  '<p>Pls refer to the attached. Thanks.'                   TO l_body_text,
    202                '</BODY><HTML>'                                             TO l_body_text.
    203       l_document = cl_document_bcs=>create_document(
    204                             i_type        = 'HTM'
    205                             i_text        = l_body_text
    206                             i_subject     = l_subject
    207                     ).
    208 
    209 *" 设置编码格式
    210 * CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
    211 *  EXPORTING
    212 *    external_name = 'UTF-8'
    213 *  IMPORTING
    214 *    sap_codepage  = lv_codepage.
    215 *
    216 *   l_encoding = lv_codepage.
    217 *
    218 *   DATA: convout TYPE REF TO cl_abap_conv_out_ce.
    219 *      " 创建编码对象
    220 *   convout = cl_abap_conv_out_ce=>create( encoding = l_encoding ).
    221 **   convout->write( data = g_datel_1 )."编码
    222 *   convout->write( data = g_datel_1 )."编码
    223 *   xstr = convout->get_buffer( )." 获取流码
    224 *
    225 *   DATA: convin TYPE REF TO cl_abap_conv_in_ce.
    226 *   convin = cl_abap_conv_in_ce=>create( encoding = l_encoding input = xstr ).
    227 *   DATA: str TYPE string.
    228 *   CALL METHOD convin->read
    229 *      importing data = str.
    230 *
    231 *      call function 'SCMS_STRING_TO_XSTRING'
    232 *      exporting
    233 *      text = str "g_datel_1
    234 **       MIMETYPE = ' '
    235 *      importing
    236 *      buffer = l_html_xstr
    237 *      exceptions 
    238 *      failed = 1 
    239 *      others = 2.       
    240 
    241 CREATE OBJECT l_zip.       
    242 CALL METHOD l_zip->add         
    243 EXPORTING           name    
    244 = '1111.xls'" 压缩附件名称  在这里不要用中文命名,会乱码           content 
    245 = lv_xstring."l_html_xstr.              
    246 
    247 CALL METHOD l_zip->add         
    248 EXPORTING           name    
    249 = '2222.xls'" 压缩附件名称  在这里不要用中文命名,会乱码           content 
    250 = lv_xstring2.       
    251 
    252 CALL METHOD l_zip->save         RECEIVING           zip 
    253 
    254 = l_zip_xstr.       
    255 
    256 CALL METHOD cl_bcs_convert=>xstring_to_solix         
    257 EXPORTING           iv_xstring 
    258 =  l_zip_xstr "lv_xstring         RECEIVING           et_solix   
    259 
    260 = l_content.        l_obj_len 
    261 
    262 = XSTRLEN( l_zip_xstr ) ."lv_xstring ). 
    263 
    264 CALL METHOD l_document->add_attachment         
    265 EXPORTING           i_attachment_size    
    266 = l_obj_len           i_attachment_type    
    267 = 'ZIP'           i_attachment_subject 
    268 = l_subject           i_att_content_hex    
    269 = l_content.       l_send_request
    270 ->set_document( l_document ).       l_sender 
    271 = cl_sapuser_bcs=>create( sy-uname ).       
    272 LOOP AT s_addr.         l_recipient 
    273 = cl_cam_address_bcs=>create_internet_address( s_addr-low ).         
    274 CALL METHOD l_send_request->add_recipient           
    275 EXPORTING             i_recipient 
    276 = l_recipient.       
    277 ENDLOOP.       
    278 CALL METHOD l_send_request->set_send_immediately( 'X' ).       
    279 CALL METHOD l_send_request->send(       
    280 EXPORTING         i_with_error_screen  
    281 = 'X'         
    282 ).       
    283 COMMIT WORK.     
    284 CATCH cx_bcs INTO l_exception.       l_message 
    285 = l_exception->get_text( ).       
    286 MESSAGE l_message TYPE 'E'.   
    287 ENDTRY.  
    288 
    289 ENDFORM

    以上excel是以上案例是以下博友合并后原创.如有更好的方法及bug问题请联系作者,谢谢

     “excel作为附加发送至邮件”

     压缩txt保存至本端”

     “压缩pdf并发送至邮箱”

  • 相关阅读:
    redis-cluster
    Mycat-主从结构的准备
    Mycat-多实例的搭建
    MSC添加shard节点
    MongoDB -MSC集群的部署
    MySQL知识-redis实例
    MySQL知识-MySQL不同版本多实例
    #Linux学习# 用户和用户组管理
    #Linux学习# 软件包管理
    #Linux学习# 文本编辑器vim
  • 原文地址:https://www.cnblogs.com/Brokenshelltao/p/11428539.html
Copyright © 2020-2023  润新知