ABAP-邮件发送本地文档,内表转Excle 作为附件发送
作者:互联网
1 *&---------------------------------------------------------------------* 2 *& Report Z3426EMAIL001 3 *&---------------------------------------------------------------------* 4 *& 5 *&---------------------------------------------------------------------* 6 REPORT z3426email001. 7 8 9 DATA:iv_mail_from TYPE ad_smtpadr, 10 iv_mail_to TYPE string, 11 iv_mail_cc TYPE string, 12 iv_mail_bc TYPE string, 13 iv_mail_title TYPE string, 14 iv_mail_body TYPE string, 15 iv_doc_type TYPE so_obj_tp VALUE 'HTM', 16 iv_attach_type TYPE so_obj_tp VALUE 'XLS'. 17 18 DATA: lc_send_request TYPE REF TO cl_bcs, 19 document TYPE REF TO cl_document_bcs, 20 lc_sender TYPE REF TO if_sender_bcs, 21 lc_recipient TYPE REF TO if_recipient_bcs, 22 sent_to_all TYPE os_boolean, 23 lc_bcs_exception TYPE REF TO cx_bcs. 24 25 DATA: lv_subject TYPE so_obj_des, 26 lv_recipient TYPE ad_smtpadr, 27 lt_mailtext TYPE soli_tab, 28 lv_message TYPE string. 29 30 DATA: BEGIN OF lt_mailto_list OCCURS 0 , 31 mail_address(50) TYPE c, 32 END OF lt_mailto_list. 33 34 iv_mail_to = 'xxxx@xxxx.com'."你的邮箱 35 iv_mail_title = 'ABAP 测试邮件'. 36 iv_mail_body = `<div>测试邮件正文</div>` && 37 `<div>测试邮件正文</div>` && 38 `<div>测试邮件正文</div>` && 39 `<div>测试邮件正文</div>` && 40 `<div>测试邮件正文</div>` && 41 `<div>测试邮件正文</div>` && 42 `<div>测试邮件正文</div>`. 43 iv_doc_type = `HTM`. 44 45 TRY. 46 *Create persistent send request 47 lc_send_request = cl_bcs=>create_persistent( ). 48 49 *Move the message from string to internal table 50 CALL FUNCTION 'SCMS_STRING_TO_FTEXT' 51 EXPORTING 52 text = iv_mail_body 53 TABLES 54 ftext_tab = lt_mailtext. 55 56 *Move the Subject from string to BCS subject type 57 lv_subject = iv_mail_title . 58 59 *Create the Document and Add To send request 60 document = cl_document_bcs=>create_document( 61 i_type = iv_doc_type 62 i_text = lt_mailtext 63 i_subject = lv_subject ). 64 65 *Add attachment 66 DATA:iv_filename TYPE string. 67 DATA:iv_xstr TYPE xstring. 68 PERFORM frm_get_xstr USING `C:\Users\XXXX\Desktop\Test\Excel文档.xlsx` CHANGING iv_xstr. 69 "PERFORM frm_get_xstr USING `C:\Users\XXXX\Desktop\Test\PDF文档.pdf.` CHANGING iv_xstr. 70 "PERFORM frm_get_xstr USING `C:\Users\XXXX\Desktop\Test\Word文档.docx'.` CHANGING iv_xstr. 71 document->add_attachment( 72 i_attachment_type = 'EXT' 73 i_att_content_hex = cl_document_bcs=>xstring_to_solix( iv_xstr ) 74 i_attachment_size = CONV #( xstrlen( iv_xstr ) ) 75 i_attachment_subject = COND #( WHEN iv_filename IS NOT INITIAL THEN iv_filename 76 ELSE CONV #( |{ sy-datum }_{ sy-uzeit }.xlsx | ) 77 ) ). 78 79 DATA:xtab TYPE solix_tab, 80 length TYPE so_att_len. 81 PERFORM frm_tabconvxtr TABLES xtab CHANGING length. 82 document->add_attachment( 83 i_attachment_type = 'EXT' 84 i_att_content_hex = xtab[] 85 i_attachment_size = length 86 i_attachment_subject = COND #( WHEN iv_filename IS NOT INITIAL THEN iv_filename 87 ELSE CONV #( |{ sy-datum }_{ sy-uzeit }_{ sy-uname }.xls | ) 88 ) ). 89 90 CALL METHOD lc_send_request->set_document( document ). 91 92 *Get and add sender object 93 IF iv_mail_from = ''. 94 lc_sender = cl_sapuser_bcs=>create( sy-uname ). 95 ELSE. 96 lc_sender = cl_cam_address_bcs=>create_internet_address( iv_mail_from ). 97 ENDIF. 98 99 CALL METHOD lc_send_request->set_sender 100 EXPORTING 101 i_sender = lc_sender. 102 103 *Get Recipient Object TO 104 REFRESH lt_mailto_list. 105 SPLIT iv_mail_to AT ';' INTO TABLE lt_mailto_list. 106 LOOP AT lt_mailto_list WHERE mail_address <> ''. 107 lv_recipient = lt_mailto_list-mail_address. 108 lc_recipient = cl_cam_address_bcs=>create_internet_address( lv_recipient ). 109 CALL METHOD lc_send_request->add_recipient 110 EXPORTING 111 i_recipient = lc_recipient 112 i_copy = ''. 113 ENDLOOP . 114 115 *Get Recipient Object CC 116 REFRESH lt_mailto_list . 117 SPLIT iv_mail_cc AT ';' INTO TABLE lt_mailto_list . 118 LOOP AT lt_mailto_list WHERE mail_address <> ''. 119 lv_recipient = lt_mailto_list-mail_address. 120 lc_recipient = cl_cam_address_bcs=>create_internet_address( lv_recipient ). 121 CALL METHOD lc_send_request->add_recipient 122 EXPORTING 123 i_recipient = lc_recipient 124 i_copy = 'X'. 125 ENDLOOP . 126 127 *Get Recipient Object BC 128 REFRESH lt_mailto_list . 129 SPLIT iv_mail_bc AT ';' INTO TABLE lt_mailto_list . 130 LOOP AT lt_mailto_list WHERE mail_address <> ''. 131 lv_recipient = lt_mailto_list-mail_address. 132 lc_recipient = cl_cam_address_bcs=>create_internet_address( lv_recipient ). 133 CALL METHOD lc_send_request->add_recipient 134 EXPORTING 135 i_recipient = lc_recipient 136 i_blind_copy = 'X'. 137 ENDLOOP . 138 139 *Set that you don't need a Return Status E-mail 140 CALL METHOD lc_send_request->set_status_attributes 141 EXPORTING 142 i_requested_status = 'E' 143 i_status_mail = 'E'. 144 145 *set send immediately flag 146 lc_send_request->set_send_immediately( 'X' ). 147 148 *Send document 149 CALL METHOD lc_send_request->send( 150 EXPORTING 151 i_with_error_screen = 'X' 152 RECEIVING 153 result = sent_to_all ). 154 IF sent_to_all = 'X'. 155 COMMIT WORK. 156 WRITE:'发送成功!'. 157 158 ELSE. 159 WRITE:'发送失败!'. 160 ENDIF. 161 162 *handle exception 163 CATCH cx_bcs INTO lc_bcs_exception. 164 lv_message = lc_bcs_exception->get_text( ). 165 WRITE:'发送失败!',lv_message. 166 167 ENDTRY. 168 169 FORM frm_get_xstr USING filename TYPE string CHANGING iv_xstr TYPE xstring. 170 DATA:lv_filter TYPE string, 171 lv_count TYPE i, 172 lv_filename TYPE string, 173 lt_file TYPE line OCCURS 0 WITH HEADER LINE, 174 lt_data TYPE STANDARD TABLE OF raw255, 175 lv_xcontent TYPE xstring. 176 lv_filename = filename. 177 CALL METHOD cl_gui_frontend_services=>gui_upload 178 EXPORTING 179 filename = lv_filename 180 filetype = 'BIN' 181 IMPORTING 182 filelength = lv_count 183 CHANGING 184 data_tab = lt_data. 185 186 CALL FUNCTION 'SCMS_BINARY_TO_XSTRING' 187 EXPORTING 188 input_length = lv_count 189 IMPORTING 190 buffer = lv_xcontent 191 TABLES 192 binary_tab = lt_data. 193 iv_xstr = lv_xcontent. 194 ENDFORM. 195 196 FORM frm_tabconvxtr TABLES pv_xtab CHANGING length. 197 DATA:gt_sflight TYPE TABLE OF sflight. 198 DATA:gt_t001l TYPE TABLE OF t001l. 199 DATA: l_binary_tab LIKE TABLE OF solix. 200 SELECT * FROM sflight INTO TABLE gt_sflight. 201 SELECT * FROM t001l INTO TABLE gt_t001l UP TO 10 ROWS. 202 DATA(l_ixml) = cl_ixml=>create( ). 203 * Creating the dom object model 创建dom对象模型 204 DATA(l_document) = l_ixml->create_document( ). 205 * Create root node 'Workbook' 创建根节点 206 DATA(l_element_root) = l_document->create_simple_element( 207 name = 'Workbook' 208 * uri = 'urn:schemas-microsoft-com:office:spreadsheet' 209 parent = l_document ). 210 l_element_root->set_attribute( name = 'xmlns' value = 'urn:schemas-microsoft-com:office:spreadsheet' ). 211 DATA(ns_attribute) = l_document->create_namespace_decl( 212 name = 'ss' 213 prefix = 'xmlns' 214 uri = 'urn:schemas-microsoft-com:office:spreadsheet' ). 215 l_element_root->set_attribute_node( ns_attribute ). 216 ns_attribute = l_document->create_namespace_decl( 217 name = 'x' 218 prefix = 'xmlns' 219 uri = 'urn:schemas-microsoft-com:office:excel' ). 220 l_element_root->set_attribute_node( ns_attribute ). 221 * Create node for document properties. 为文档属性创建节点 222 DATA(r_element_properties) = l_document->create_simple_element( 223 name = 'DocumentProperties' 224 parent = l_element_root ). 225 DATA:l_value TYPE string VALUE '陈辉'. 226 DATA:lv_sheetname TYPE string VALUE 'Sheet1'. 227 l_value = sy-uname. 228 l_document->create_simple_element( 229 name = 'Author' 230 value = l_value 231 parent = r_element_properties ). 232 233 FIELD-SYMBOLS: 234 <lft_dyntab> TYPE ANY TABLE. 235 "2.创建excel表名和sheet数据 236 237 ASSIGN gt_sflight TO <lft_dyntab>. 238 lv_sheetname = '自定义sheet1'. 239 240 PERFORM frm_create_sheetdata USING <lft_dyntab> 241 l_document 242 l_element_root 243 lv_sheetname. 244 245 lv_sheetname = '自定义sheet2'. 246 ASSIGN gt_t001l TO <lft_dyntab>. 247 PERFORM frm_create_sheetdata USING <lft_dyntab> 248 l_document 249 l_element_root 250 lv_sheetname. 251 252 "xml生成输出 253 DATA(l_streamfactory) = l_ixml->create_stream_factory( ). 254 * Connect internal XML table to stream factory 255 DATA(l_ostream) = l_streamfactory->create_ostream_itable( table = l_binary_tab ). 256 * rendering the document 257 DATA(l_renderer) = l_ixml->create_renderer( ostream = l_ostream document = l_document ). 258 DATA(l_rc) = l_renderer->render( ). 259 * saving the xml document 260 DATA(l_length) = l_ostream->get_num_written_raw( ). 261 262 pv_xtab[] = l_binary_tab[]. 263 length = l_length. 264 ENDFORM. 265 *&---------------------------------------------------------------------* 266 *& Form FRM_CREATE_SHEETDATA 267 *&---------------------------------------------------------------------* 268 * text 269 *----------------------------------------------------------------------* 270 * --> p1 text 271 * <-- p2 text 272 *----------------------------------------------------------------------* 273 FORM frm_create_sheetdata USING lt_dyntab TYPE ANY TABLE 274 l_document TYPE REF TO if_ixml_document 275 l_element_root TYPE REF TO if_ixml_element 276 lv_sheetname TYPE string. 277 DATA:l_value TYPE string. 278 DATA(r_worksheet) = l_document->create_simple_element( 279 name = 'Worksheet ' 280 parent = l_element_root ). 281 r_worksheet->set_attribute_ns( 282 name = 'Name' 283 prefix = 'ss' 284 value = lv_sheetname ). 285 * table 286 * <Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="1" x:FullColumns="1" x:FullRows="1"> 287 DATA(r_table) = l_document->create_simple_element( 288 name = 'Table' 289 parent = r_worksheet ). 290 291 "3.创建column 292 293 "因为xml格式的excel为先column,然后再每个row下存在cell,所以先循环输出column。 294 295 DATA(r_column) = l_document->create_simple_element( 296 name = 'Column' 297 parent = r_table ). 298 299 "4.row和内容类型 300 DATA:l_row TYPE REF TO if_ixml_element, 301 l_cell TYPE REF TO if_ixml_element, 302 l_data TYPE REF TO if_ixml_element. 303 304 "将内表填写到excel表格内 305 DATA:i_index TYPE sy-index. 306 DATA:l_index TYPE string. 307 DATA:l_text TYPE char10. 308 DATA:l_type TYPE string. 309 DATA:desc_struc TYPE REF TO cl_abap_structdescr. 310 FIELD-SYMBOLS:<lv_field> TYPE any. 311 LOOP AT lt_dyntab ASSIGNING FIELD-SYMBOL(<data_line>). 312 desc_struc ?= cl_abap_structdescr=>describe_by_data( <data_line> )."因为内表的每列数据要填写到excel的每行内。所以使用该方法将内表转换。 313 DATA(lt_fields) = desc_struc->get_ddic_field_list( ). 314 315 AT FIRST. 316 l_row = l_document->create_simple_element( 317 name = 'Row' 318 parent = r_table ). 319 LOOP AT lt_fields ASSIGNING FIELD-SYMBOL(<data_fields>). 320 * ASSIGN COMPONENT <data_fields>-fieldname OF STRUCTURE <data_line> TO <lv_field>."动态绑定表格内容。 321 CHECK sy-subrc IS INITIAL. 322 l_cell = l_document->create_simple_element( 323 name = 'Cell' 324 parent = l_row ). 325 l_value = <data_fields>-scrtext_l. 326 l_data = l_document->create_simple_element( 327 name = 'Data' 328 value = l_value 329 parent = l_cell ). 330 l_data->set_attribute_ns( 331 name = 'Type' 332 prefix = 'ss' 333 value = 'String' ). 334 ENDLOOP. 335 ENDAT. 336 l_row = l_document->create_simple_element( 337 name = 'Row' 338 parent = r_table ). 339 340 LOOP AT lt_fields ASSIGNING <data_fields>. 341 ASSIGN COMPONENT <data_fields>-fieldname OF STRUCTURE <data_line> TO <lv_field>."动态绑定表格内容。 342 CHECK sy-subrc IS INITIAL. 343 l_cell = l_document->create_simple_element( 344 name = 'Cell' 345 parent = l_row ). 346 l_value = <lv_field>. 347 l_type = <data_fields>-inttype. "fieldname, scrtext_m etc. 348 CASE l_type. 349 WHEN 'I' OR 'P' OR 'F' OR 'N'. 350 l_type = 'String'. 351 l_value = <lv_field>. 352 CONDENSE l_value NO-GAPS. 353 WHEN 'D' OR 'T'. 354 l_type = 'String'. 355 WRITE <lv_field> TO l_text. 356 l_value = l_text. 357 WHEN OTHERS. 358 * l_value = <field>. "Without conversion exit 359 WRITE <lv_field> TO l_text. 360 SHIFT l_text LEFT DELETING LEADING space. 361 l_value = l_text. 362 l_type = 'String'. 363 ENDCASE. 364 l_data = l_document->create_simple_element( 365 name = 'Data' 366 value = l_value 367 parent = l_cell ). 368 l_data->set_attribute_ns( 369 name = 'Type' 370 prefix = 'ss' 371 value = l_type ). 372 ENDLOOP. 373 ENDLOOP. 374 ENDFORM.
内表转Xstring其中参考:https://www.cnblogs.com/liangliang245916893/p/15151576.html
标签:lv,DATA,create,iv,发送,ABAP,内表转,document,TYPE 来源: https://www.cnblogs.com/1187163927ch/p/16334676.html