abap 发送附件 excel 解决 打开附件提示格式不一致问题

Posted 风口的猪会飞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了abap 发送附件 excel 解决 打开附件提示格式不一致问题相关的知识,希望对你有一定的参考价值。

旧语法版本

*&---------------------------------------------------------------------*
*& Report  ZTEST_EMAIL_XLSX
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ztest_email_xlsx.

DATA:lt_spfli TYPE TABLE OF spfli.

SELECT * UP TO 100 ROWS
  FROM spfli
  INTO TABLE lt_spfli.

DATA lr_table TYPE REF TO cl_salv_table.
cl_salv_table=>factory( IMPORTING r_salv_table = lr_table
                        CHANGING t_table = lt_spfli ).

DATA: lr_xldimension TYPE REF TO if_ixml_node,
      lr_xlworksheet TYPE REF TO if_ixml_element.

DATA:lv_xlsx TYPE xstring.


lv_xlsx  = lr_table->to_xml( if_salv_bs_xml=>c_type_xlsx ).

DATA:lr_zip TYPE REF TO cl_abap_zip.
CREATE OBJECT lr_zip.

lr_zip->load( lv_xlsx ).
DATA:lv_file TYPE xstring.

lr_zip->get( EXPORTING name = \'xl/worksheets/sheet1.xml\' IMPORTING content = lv_file ).

DATA:lr_file TYPE REF TO cl_xml_document.
CREATE OBJECT lr_file.
lr_file->parse_xstring( lv_file ).

DATA:lr_xlnode TYPE REF TO if_ixml_node.
lr_xlnode = lr_file->find_node( \'sheetData\' ).

DATA:lr_xlrows TYPE REF TO if_ixml_node_list.

lr_xlrows = lr_xlnode->get_children( ).

lr_xlworksheet ?= lr_file->find_node( \'worksheet\' ).

DATA:lr_xlsheetpr TYPE REF TO if_ixml_element.

lr_xlsheetpr   = cl_ixml=>create( )->create_document( )->create_element( name = \'sheetPr\' ).

DATA:lr_xloutlinepr TYPE REF TO if_ixml_element.
lr_xloutlinepr = cl_ixml=>create( )->create_document( )->create_element( name = \'outlinePr\' ).

lr_xlsheetpr->if_ixml_node~append_child( lr_xloutlinepr ).
lr_xloutlinepr->set_attribute( name = \'summaryBelow\' value = \'false\' ).
lr_xldimension ?= lr_file->find_node( \'dimension\' ).
lr_xlworksheet->if_ixml_node~insert_child( new_child = lr_xlsheetpr ref_child = lr_xldimension ).


lr_file->render_2_xstring( IMPORTING stream = lv_file ).
lr_zip->delete( EXPORTING name = \'xl/worksheets/sheet1.xml\' ).
lr_zip->add( EXPORTING name = \'xl/worksheets/sheet1.xml\' content = lv_file ).
lv_xlsx = lr_zip->save( ).

DATA lv_size     TYPE i.
DATA lt_bintab TYPE solix_tab.

* Convert to binary
CALL FUNCTION \'SCMS_XSTRING_TO_BINARY\'
  EXPORTING
    buffer        = lv_xlsx
  IMPORTING
    output_length = lv_size
  TABLES
    binary_tab    = lt_bintab.

DATA main_text      TYPE bcsy_text.

DATA:send_request TYPE REF TO cl_bcs.
send_request = cl_bcs=>create_persistent( ).

DATA:document TYPE REF TO cl_document_bcs.
APPEND \'Valid Excel file\' TO main_text.
document = cl_document_bcs=>create_document( i_type = \'RAW\' i_text = main_text i_subject = \'Test Created for stella\' ).
DATA:lv_objlen TYPE so_obj_len.



DATA lt_att_head TYPE soli_tab.
APPEND \'<(>&< )>SO_FILENAME=MySheet.xlsx\' TO lt_att_head.

lv_objlen = lv_size.
CONDENSE lv_objlen NO-GAPS.

* add the spread sheet as attachment to document object
document->add_attachment(
  i_attachment_type    = \'xls\'
  i_attachment_subject = \'MySheet\'
  i_attachment_size    =  lv_objlen
  i_attachment_header  = lt_att_head
  i_att_content_hex    = lt_bintab ).

send_request->set_document( document ).

DATA:recipient TYPE REF TO cl_cam_address_bcs.
recipient = cl_cam_address_bcs=>create_internet_address( \'383981024@qq.com\' ).

send_request->add_recipient( recipient ).

DATA:sent_to_all TYPE  os_boolean.
sent_to_all = send_request->send( i_with_error_screen = \'X\' ).


COMMIT WORK.

新语法版本

*&---------------------------------------------------------------------*
*& Report ZTEST_EMAIL2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_email2.
SELECT * UP TO 100 ROWS
  FROM spfli
  INTO TABLE @DATA(lt_spfli).

cl_salv_table=>factory( IMPORTING r_salv_table = DATA(lr_table)
                        CHANGING t_table = lt_spfli ).

DATA: lr_xldimension TYPE REF TO if_ixml_node,
      lr_xlworksheet TYPE REF TO if_ixml_element.

DATA(lv_xlsx) = lr_table->to_xml( if_salv_bs_xml=>c_type_xlsx ).
DATA(lr_zip) = NEW cl_abap_zip( ).
lr_zip->load( lv_xlsx ).
lr_zip->get( EXPORTING name = \'xl/worksheets/sheet1.xml\' IMPORTING content = DATA(lv_file) ).

DATA(lr_file) = NEW cl_xml_document( ).
lr_file->parse_xstring( lv_file ).
* Row elements are under SheetData
DATA(lr_xlnode) = lr_file->find_node( \'sheetData\' ).
DATA(lr_xlrows) = lr_xlnode->get_children( ).
* Create new element in the XML file
lr_xlworksheet ?= lr_file->find_node( \'worksheet\' ).
DATA(lr_xlsheetpr)   = cl_ixml=>create( )->create_document( )->create_element( name = \'sheetPr\' ).
DATA(lr_xloutlinepr) = cl_ixml=>create( )->create_document( )->create_element( name = \'outlinePr\' ).
lr_xlsheetpr->if_ixml_node~append_child( lr_xloutlinepr ).
lr_xloutlinepr->set_attribute( name = \'summaryBelow\' value = \'false\' ).
lr_xldimension ?= lr_file->find_node( \'dimension\' ).
lr_xlworksheet->if_ixml_node~insert_child( new_child = lr_xlsheetpr ref_child = lr_xldimension ).
* Create xstring and move it to XLSX
lr_file->render_2_xstring( IMPORTING stream = lv_file ).
lr_zip->delete( EXPORTING name = \'xl/worksheets/sheet1.xml\' ).
lr_zip->add( EXPORTING name = \'xl/worksheets/sheet1.xml\' content = lv_file ).
lv_xlsx = lr_zip->save( ).

DATA lv_size     TYPE i.
DATA lt_bintab TYPE solix_tab.

* Convert to binary
CALL FUNCTION \'SCMS_XSTRING_TO_BINARY\'
  EXPORTING
    buffer        = lv_xlsx
  IMPORTING
    output_length = lv_size
  TABLES
    binary_tab    = lt_bintab.

DATA main_text      TYPE bcsy_text.
* create persistent send request
DATA(send_request) = cl_bcs=>create_persistent( ).
* create document object from internal table with text
APPEND \'Valid Excel file\' TO main_text.
DATA(document) = cl_document_bcs=>create_document( i_type = \'RAW\' i_text = main_text i_subject = \'Test Created for stella\' ).

DATA lt_att_head TYPE soli_tab.
APPEND \'<(>&< )>SO_FILENAME=MySheet.xlsx\' TO lt_att_head.
* add the spread sheet as attachment to document object
document->add_attachment(
  i_attachment_type    = \'xls\'
  i_attachment_subject = \'MySheet\'
  i_attachment_size    = CONV so_obj_len( lv_size )
  i_attachment_header  = lt_att_head
  i_att_content_hex    = lt_bintab ).

send_request->set_document( document ).
DATA(recipient) = cl_cam_address_bcs=>create_internet_address( \'some_recipient@mail.com\' ).
send_request->add_recipient( recipient ).
DATA(sent_to_all) = send_request->send( i_with_error_screen = \'X\' ).

COMMIT WORK.

把新语法还原到旧版本是一个苦逼的活。

以上是关于abap 发送附件 excel 解决 打开附件提示格式不一致问题的主要内容,如果未能解决你的问题,请参考以下文章

ABAP 通过程序向外部邮箱发送邮件时生成的Excel附件能否设置格式?怎么设置

如何使用 Excel VBA 打开 Outlook excel 附件,在特定时间范围内发送到特定 Outlook 文件夹?

通过 Gmail API 发送 Excel 文件,但附件已损坏

在网页中打开word和excel文件时提示文件已损坏,无法打开的解决办法

SAP附件UI里选择的文件是如何传到ABAP服务器的

火狐浏览器打开附件提示潜在的危险内容被阻止