Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Sending email with .xlsx attachment

Former Member

Hello experts,

Currently I am using FM SO_DOCUMENT_SEND_API1 to send emails with a .xls attachment. It is working fine. However, I would like to send a .xlsx attachment but the format data type is char 3. Therefore, this FM is no longer useful for me. I have already read many threads related to abap2xlsx and CL_BCS/CL_DOCUMENT_BCS. Is any way to do this?

Thanks in advance, regards.

P.D: I found this link helpful: Attaching xlsx files to email with CL_BCS and C... | SCN

1 ACCEPTED SOLUTION

Former Member

Hello,

Thank you for your answers. Finally I found out how to attach an excel .xlsx in an email. Then the attachment can be opened. The following program, SAP note and the link below were really useful:

The code I implemented is the following:

REPORT z_send_xlsx.

* This report provides an example for sending an Excel
* attachment in Unicode Systems

CONSTANTS:
gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab,
gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf.

PARAMETERSmailto TYPE ad_smtpadr
DEFAULT 'supersap@gmail.com'.

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: g_xstring TYPE xstring.

START-OF-SELECTION.
PERFORM create_content.
PERFORM send.

*&---------------------------------------------------------------------*
*&      Form send
*&---------------------------------------------------------------------*
FORM send.

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.
document = cl_document_bcs=>create_document(
i_type    = 'RAW'
i_text    = main_text
i_subject = 'Test Created By BCS_EXAMPLE_7' ).

*     add the spread sheet as attachment to document object

DATA lt_att_head    TYPE soli_tab.
DATA lv_filename    TYPE string.
DATA lv_text_line    TYPE soli.
lv_filename = 'excelexample.xlsx'.
CONCATENATE '&SO_FILENAME=' lv_filename INTO lv_text_line.
APPEND lv_text_line TO lt_att_head.

document->add_attachment(
i_attachment_type    = 'BIN'
i_attachment_subject = 'ExampleSpreadSheet'
i_attachment_size    = '21'
i_att_content_hex    = binary_content
i_attachment_header  = lt_att_head ).

*     add document object to send request
send_request->set_document( document ).

*     --------- add recipient (e-mail address) -----------------------
*     create recipient object
recipient = cl_cam_address_bcs=>create_internet_address( mailto ).

*     add recipient object to send request
send_request->add_recipient( recipient ).

*     ---------- send document ---------------------------------------
sent_to_all = send_request->send( i_with_error_screen = 'X' ).

COMMIT WORK.

IF sent_to_all IS INITIAL.
MESSAGE i500(sbcoms) WITH mailto.
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. "send

*&---------------------------------------------------------------------*
*&      Form create_content
*&---------------------------------------------------------------------*
* Create Example Content
* 1) Write example text into a string
* 2) convert this string to solix_tab
*----------------------------------------------------------------------*
FORM create_content.

DATA: gt_spfli TYPE STANDARD TABLE OF spfli.
DATA: mt_fcat TYPE lvc_t_fcat.
DATA: mt_data       TYPE REF TO data.
DATA: m_flavour TYPE string.
DATA: m_version TYPE string.
DATA: mo_result_data TYPE REF TO cl_salv_ex_result_data_table.
DATA: mo_columns  TYPE REF TO cl_salv_columns_table.
DATA: mo_aggreg   TYPE REF TO cl_salv_aggregations.
DATA: mo_salv_table  TYPE REF TO cl_salv_table.
DATA: m_file_type TYPE salv_bs_constant.
FIELD-SYMBOLS <tab> TYPE ANY TABLE.

SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE gt_spfli.

GET REFERENCE OF gt_spfli INTO mt_data.

*if we didn't pass fieldcatalog we need to create it
ASSIGN mt_data->* TO <tab>.
TRY .
cl_salv_table=>factory(
EXPORTING
list_display = abap_false
IMPORTING
r_salv_table = mo_salv_table
CHANGING
t_table      = <tab> ).
CATCH cx_salv_msg.

ENDTRY.
"get colums & aggregation infor to create fieldcat
mo_columns  = mo_salv_table->get_columns( ).
mo_aggreg   = mo_salv_table->get_aggregations( ).
mt_fcat     cl_salv_controller_metadata=>get_lvc_fieldcatalog(
  r_columns      = mo_columns
  r_aggregations = mo_aggreg ).

IF cl_salv_bs_a_xml_base=>get_version( ) EQ
if_salv_bs_xml=>version_25 OR
cl_salv_bs_a_xml_base=>get_version( ) EQ
if_salv_bs_xml=>version_26.

mo_result_data = cl_salv_ex_util=>factory_result_data_table(
r_data                      = mt_data
*        s_layout                    = is_layout
t_fieldcatalog              = mt_fcat
*        t_sort                      = it_sort
*        t_filter                    = it_filt
).

CASE cl_salv_bs_a_xml_base=>get_version( ).
WHEN if_salv_bs_xml=>version_25.
m_version = if_salv_bs_xml=>version_25.
WHEN if_salv_bs_xml=>version_26.
m_version = if_salv_bs_xml=>version_26.
ENDCASE.

m_file_type = if_salv_bs_xml=>c_type_xlsx.

m_flavour = if_salv_bs_c_tt=>c_tt_xml_flavour_export.
"transformation of data to excel
CALL METHOD cl_salv_bs_tt_util=>if_salv_bs_tt_util~transform
EXPORTING
xml_type      = m_file_type
xml_version   = m_version
r_result_data = mo_result_data
xml_flavour   = m_flavour
gui_type      = if_salv_bs_xml=>c_gui_type_gui
IMPORTING
xml           = g_xstring.
ENDIF.

CALL METHOD cl_bcs_convert=>xstring_to_solix
EXPORTING
iv_xstring = g_xstring
RECEIVING
et_solix   = binary_content.

ENDFORM. "create_content

17 REPLIES 17

sanjeev_mishra_15aug
Active Participant
0 Kudos

This message was moderated.

matt
Active Contributor
0 Kudos

I don't understand what you are wanting? The answer is use the CL_BCS classes and Abap2xlsx to construct the Excel file. And you've already found a helpful link.

Former Member
0 Kudos

Hello,

I am developing a test program to send emails with a .xlsx attachment. I am also following this link: Sending mail with attachment document type .doc... | SCN. However, it is not working fine. When I try to open the document attached, a error message is populated reading that file cannot be open. What am I doing wrong? Can you provide a sample code which works?

Thanks.

matt
Active Contributor
0 Kudos

What is the exact error message?

Please don't ask for sample code. There's plenty of examples, and I managed to figure out what to do from those.

Former Member
0 Kudos

I attach both error messages. Messages read that file is damaged and cannot be open.But I can open it using WordPad.

matt
Active Contributor
0 Kudos

But your original question says you want an xlsx file. So why are you working with docx?

Former Member
0 Kudos

Sorry, you are right. I found a thread with a sample code for .docx. I tried this code with .xlsx documents and the result is the same. I mean, when I create and send a .xlsx document the error messages are the same.

Regards.

former_member197916
Active Participant
0 Kudos

hi,

check standard program BCS_EXAMPLE_7 and other BCS_EXAMPLE* programs.

hope it helps

0 Kudos

Hello,

Thank you for your answer. I run BCS_EXAMPLE_7 and send emails properly. I tried to modify data in debugging mode to crate a .xlsx document. The problem is that I am facing an error when opening it. I cannot open the file.



Regards.

0 Kudos

hi,

I think it has something to do with MS Excel rather than ABAP.You can try to:

To check the support of xlsx extension, go to 'My Computer'. Browse 'Tools >> Folder Options >> File Types'. Check if 'xlsx' extension is included there or not. If not, then rename this file and change '.xlsx' to '.xls'.

or

Go to the 'Open' drop-down provided in the 'Open' dialog box, and then select 'Open and Repair' option from there. This process repairs the corrupt Excel sheet and opens it normally.

0 Kudos

I really doubt that you have valid xlsx file, if you have changed extension in debugger it does not change the format of the file....

Your problem seems not to be how to send the xlsx file, but how to create one in abap. And that's another story covered hundreds times in scn.

0 Kudos

Hello,

You are right. The error was in the creation of the .xlsx document.

Regards.

0 Kudos

This message was moderated.

Jelena
Active Contributor
0 Kudos

Google -> attach xlsx to email site:sap.com -> finds 207 posts, some have code included as well.


I'm also confused what exactly is the issue here... You need to get XLSX content somehow (it's not clear so far where exactly it'd be coming from) and then just use the above mentioned class to send the email. Attachment is added as binary content.

How could the SCN members possibly know what manipulation have you done in debugger and why a sample is not working afterwards?

Former Member
0 Kudos

Hello,

The .xlsx document come from an internal table. I manipulated variables in debugging mode according to SAP note: 1459896.

Regards.

Former Member

Hello,

Thank you for your answers. Finally I found out how to attach an excel .xlsx in an email. Then the attachment can be opened. The following program, SAP note and the link below were really useful:

The code I implemented is the following:

REPORT z_send_xlsx.

* This report provides an example for sending an Excel
* attachment in Unicode Systems

CONSTANTS:
gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab,
gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf.

PARAMETERSmailto TYPE ad_smtpadr
DEFAULT 'supersap@gmail.com'.

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: g_xstring TYPE xstring.

START-OF-SELECTION.
PERFORM create_content.
PERFORM send.

*&---------------------------------------------------------------------*
*&      Form send
*&---------------------------------------------------------------------*
FORM send.

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.
document = cl_document_bcs=>create_document(
i_type    = 'RAW'
i_text    = main_text
i_subject = 'Test Created By BCS_EXAMPLE_7' ).

*     add the spread sheet as attachment to document object

DATA lt_att_head    TYPE soli_tab.
DATA lv_filename    TYPE string.
DATA lv_text_line    TYPE soli.
lv_filename = 'excelexample.xlsx'.
CONCATENATE '&SO_FILENAME=' lv_filename INTO lv_text_line.
APPEND lv_text_line TO lt_att_head.

document->add_attachment(
i_attachment_type    = 'BIN'
i_attachment_subject = 'ExampleSpreadSheet'
i_attachment_size    = '21'
i_att_content_hex    = binary_content
i_attachment_header  = lt_att_head ).

*     add document object to send request
send_request->set_document( document ).

*     --------- add recipient (e-mail address) -----------------------
*     create recipient object
recipient = cl_cam_address_bcs=>create_internet_address( mailto ).

*     add recipient object to send request
send_request->add_recipient( recipient ).

*     ---------- send document ---------------------------------------
sent_to_all = send_request->send( i_with_error_screen = 'X' ).

COMMIT WORK.

IF sent_to_all IS INITIAL.
MESSAGE i500(sbcoms) WITH mailto.
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. "send

*&---------------------------------------------------------------------*
*&      Form create_content
*&---------------------------------------------------------------------*
* Create Example Content
* 1) Write example text into a string
* 2) convert this string to solix_tab
*----------------------------------------------------------------------*
FORM create_content.

DATA: gt_spfli TYPE STANDARD TABLE OF spfli.
DATA: mt_fcat TYPE lvc_t_fcat.
DATA: mt_data       TYPE REF TO data.
DATA: m_flavour TYPE string.
DATA: m_version TYPE string.
DATA: mo_result_data TYPE REF TO cl_salv_ex_result_data_table.
DATA: mo_columns  TYPE REF TO cl_salv_columns_table.
DATA: mo_aggreg   TYPE REF TO cl_salv_aggregations.
DATA: mo_salv_table  TYPE REF TO cl_salv_table.
DATA: m_file_type TYPE salv_bs_constant.
FIELD-SYMBOLS <tab> TYPE ANY TABLE.

SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE gt_spfli.

GET REFERENCE OF gt_spfli INTO mt_data.

*if we didn't pass fieldcatalog we need to create it
ASSIGN mt_data->* TO <tab>.
TRY .
cl_salv_table=>factory(
EXPORTING
list_display = abap_false
IMPORTING
r_salv_table = mo_salv_table
CHANGING
t_table      = <tab> ).
CATCH cx_salv_msg.

ENDTRY.
"get colums & aggregation infor to create fieldcat
mo_columns  = mo_salv_table->get_columns( ).
mo_aggreg   = mo_salv_table->get_aggregations( ).
mt_fcat     cl_salv_controller_metadata=>get_lvc_fieldcatalog(
  r_columns      = mo_columns
  r_aggregations = mo_aggreg ).

IF cl_salv_bs_a_xml_base=>get_version( ) EQ
if_salv_bs_xml=>version_25 OR
cl_salv_bs_a_xml_base=>get_version( ) EQ
if_salv_bs_xml=>version_26.

mo_result_data = cl_salv_ex_util=>factory_result_data_table(
r_data                      = mt_data
*        s_layout                    = is_layout
t_fieldcatalog              = mt_fcat
*        t_sort                      = it_sort
*        t_filter                    = it_filt
).

CASE cl_salv_bs_a_xml_base=>get_version( ).
WHEN if_salv_bs_xml=>version_25.
m_version = if_salv_bs_xml=>version_25.
WHEN if_salv_bs_xml=>version_26.
m_version = if_salv_bs_xml=>version_26.
ENDCASE.

m_file_type = if_salv_bs_xml=>c_type_xlsx.

m_flavour = if_salv_bs_c_tt=>c_tt_xml_flavour_export.
"transformation of data to excel
CALL METHOD cl_salv_bs_tt_util=>if_salv_bs_tt_util~transform
EXPORTING
xml_type      = m_file_type
xml_version   = m_version
r_result_data = mo_result_data
xml_flavour   = m_flavour
gui_type      = if_salv_bs_xml=>c_gui_type_gui
IMPORTING
xml           = g_xstring.
ENDIF.

CALL METHOD cl_bcs_convert=>xstring_to_solix
EXPORTING
iv_xstring = g_xstring
RECEIVING
et_solix   = binary_content.

ENDFORM. "create_content

Andrea14
Newcomer
0 Kudos

how to add data export to excel XLS version or make XLS standard instead of XLSX.

Andrea14_0-1711364048296.png