You are on page 1of 13

Formatted Excel in Email Attachment

Sharing Knowledge


Author: Gursimran Singh Gujral

Sharing Knowledge Formatted Excel in Email Attachment
2 | P a g e

Contents
Introduction .................................................................................................................................................. 3
Prerequisites ................................................................................................................................................. 3
Overview ....................................................................................................................................................... 3
Example ......................................................................................................................................................... 4
About Author .............................................................................................................................................. 13


Sharing Knowledge Formatted Excel in Email Attachment
3 | P a g e

Introduction

If we have requirement of exporting data to a Formatted Excel spreadsheet and sending as an attachment in email
we can either use OLE (Object Linking & Enabling) or XML method.
As OLE is not available below ECC 6.0 so in this document we will be covering XML method in detail. However
knowledge & Basics of XML will not be in scope of this document.
Prerequisites

Knowledge of SAP ABAP
Basics of XML

Overview

Create Custom i.e. z table for maintaining email id
Create Executable Report
Declare Global Variables
Fetch Data from DDIC tables eg. MARA to final internal table
Fetch email id from custom table
Prepare XML generate excel
Sending Email


Sharing Knowledge Formatted Excel in Email Attachment
4 | P a g e

Example

*Note:- I have created example on SAP 4.7 version and is compatible upwards.

*&---------------------------------------------------------------------*
*& Report ZMM_Material_Details
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZMM_Material_Details.
----------------------------------------------------------------------------------------------------------
----------Global Declaration
----------------------------------------------------------------------------------------------------------
TYPE-POOLS: ixml. XML data modeling

Declaration structure of final table
Types: Begin of ty_mara,
MATNR type mara-matnr,
ERSDA type mara-ersda,
ERNAM type mara-ernam,
MTART type mara-mtart,
End of ty_mara,

BEGIN OF ty_xml_line,
data(255) TYPE x,
END OF xml_line.

Declaration of internal table and work area
Data: gt_final Type table of ty_mara,
gt_xml_table TYPE TABLE OF ty_xml_line,
gt_material_e type table of zmm_material_email,
gw_final Type ty_mara,
gw_xml_table Type ty_xml_line,
gw_material_e type zmm_material_email,
gv_mail_success type c,
gv_no_email type c,
gv_xml_size TYPE i,
gv_rc TYPE i.

Sharing Knowledge Formatted Excel in Email Attachment
5 | P a g e

START-OF-SELECTION.

Selection of data from MARA table
Select matnr
Ersda
Ernam
Mtart
From mara
Into table gt_final
Up To 100 rows.
If sy-subrc EQ 0.
Else.
Handle error/exception.
Endif.

Selection of data from zmm_material_email
Select email
From zmm_material_email
Into table gt_material_e
Where active EQ c_x.
If sy-subrc EQ 0.
Else.
Handle error/exception.
Endif.


End-of-selection.

Perform Process_xml_data.

Perform send_mail.

If gv_no_email is not initial.
Display some error message.
Endif.

If gv_mail_success is not initial.
Display some success message.
Endif.

Sharing Knowledge Formatted Excel in Email Attachment
6 | P a g e

*&---------------------------------------------------------------------*
*& Form process_xml_data
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM process_xml_data .


DATA: lc_ixml TYPE REF TO if_ixml,
lc_streamfactory TYPE REF TO if_ixml_stream_factory,
lc_ostream TYPE REF TO if_ixml_ostream,
lc_renderer TYPE REF TO if_ixml_renderer,
lc_document TYPE REF TO if_ixmlc_document .

DATA: lc_element_root TYPE REF TO if_ixml_element,
lc_attribute TYPE REF TO if_ixml_attribute,
rc_element_properties TYPE REF TO if_ixml_element,
rc_element TYPE REF TO if_ixml_element,
rc_worksheet TYPE REF TO if_ixml_element,
rc_table TYPE REF TO if_ixml_element,
rc_column TYPE REF TO if_ixml_element,
rc_row TYPE REF TO if_ixml_element,
rc_cell TYPE REF TO if_ixml_element,
rc_data TYPE REF TO if_ixml_element,
lv_value TYPE string,
lv_type TYPE string,
lv_text(100) TYPE c,
rc_styles TYPE REF TO if_ixml_element,
rc_style TYPE REF TO if_ixml_element,
rc_style1 TYPE REF TO if_ixml_element,
rc_format TYPE REF TO if_ixml_element,
rc_border TYPE REF TO if_ixml_element,
lv_num_rows TYPE i,
lv_style TYPE string.

* Creating a ixml Factory
lc_ixml = clc_ixml=>create( ).

* Creating the DOM Object Model
lc_document = lc_ixml->create_document( ).

* Create Root Node 'Workbook'
lc_element_root = lc_document ->create_simple_element( name = 'Workbook' parent = lc_document ).

*Defining Attributes
lc_element_root->set_attribute( name = 'xmlns' value = 'urn:schemas-microsoft-com:office:spreadsheet' ).

lc_attribute = lc_document ->create_namespace_decl( name = 'ss' prefix = 'xmlns' uri = 'urn:schemas-microsoft-
com:office:spreadsheet' ).

Sharing Knowledge Formatted Excel in Email Attachment
7 | P a g e

lc_element_root->set_attribute_node( lc_attribute ).

lc_attribute = lc_document ->create_namespace_decl( name = 'x' prefix = 'xmlns' uri = 'urn:schemas-microsoft-
com:office:excel' ).
lc_element_root->set_attribute_node( lc_attribute ).

* Create node for document properties.
rc_element_properties = lc_document ->create_simple_element( name = 'Material Details' parent =
lc_element_root ).


Author of document
lv_value = sy-uname.
lc_document ->create_simple_element( name = 'Author' value = lv_value parent = rc_element_properties ).

* Styles for excel
rc_styles = lc_document ->create_simple_element( name = 'Styles' parent = lc_element_root ).


* Style for Header
rc_style = lc_document ->create_simple_element( name = 'Style' parent = rc_styles ).
rc_style->set_attribute_ns( name = 'ID' prefix = 'ss' value = 'Header' ).


Font settings for style name header
rc_format = lc_document ->create_simple_element( name = 'Font' parent = rc_style ).
rc_format->set_attribute_ns( name = 'Bold' prefix = 'ss' value = '1' ).

Background Color & Pattern
rc_format = lc_document ->create_simple_element( name = 'Interior' parent = rc_style ).
rc_format->set_attribute_ns( name = 'Color' prefix = 'ss' value = '#A9A9A9' ).
rc_format->set_attribute_ns( name = 'Pattern' prefix = 'ss' value = 'Solid' ).

Vertical alignment
rc_format = lc_document ->create_simple_element( name = 'Alignment' parent = rc_style ).
rc_format->set_attribute_ns( name = 'Vertical' prefix = 'ss' value = 'Center' ).
rc_format->set_attribute_ns( name = 'WrapText' prefix = 'ss' value = '1' ).

Border bottom
rc_border = lc_document ->create_simple_element( name = 'Borders' parent = rc_style ).
rc_format = lc_document ->create_simple_element( name = 'Border' parent = rc_border ).
rc_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Bottom' ).
rc_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
rc_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).

Border Left
rc_format = lc_document ->create_simple_element( name = 'Border' parent = rc_border ).
rc_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Left' ).
rc_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
rc_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).

Border Top
rc_format = lc_document ->create_simple_element( name = 'Border' parent = rc_border ).
Sharing Knowledge Formatted Excel in Email Attachment
8 | P a g e

rc_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Top' ).
rc_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
rc_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).


Border Right
rc_format = lc_document ->create_simple_element( name = 'Border' parent = rc_border ).
rc_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Right' ).
rc_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
rc_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).

* Style for Data
rc_style1 = lc_document ->create_simple_element( name = 'Style' parent = rc_styles ).
rc_style1->set_attribute_ns( name = 'ID' prefix = 'ss' value = 'Data' ).

rc_border = lc_document ->create_simple_element( name = 'Borders' parent = rc_style1 ).
rc_format = lc_document ->create_simple_element( name = 'Border' parent = rc_border ).
rc_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Bottom' ).
rc_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
rc_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).

rc_format = lc_document ->create_simple_element( name = 'Border' parent = rc_border ).
rc_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Left' ).
rc_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
rc_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).

rc_format = lc_document ->create_simple_element( name = 'Border' parent = rc_border ).
rc_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Top' ).
rc_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
rc_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).

rc_format = lc_document ->create_simple_element( name = 'Border' parent = rc_border ).
rc_format->set_attribute_ns( name = 'Position' prefix = 'ss' value = 'Right' ).
rc_format->set_attribute_ns( name = 'LineStyle' prefix = 'ss' value = 'Continuous' ).
rc_format->set_attribute_ns( name = 'Weight' prefix = 'ss' value = '1' ).

* Worksheet attributes
rc_worksheet = lc_document ->create_simple_element( name = 'Worksheet' parent = lc_element_root ).
r_worksheet->set_attribute_ns( name = 'Name' prefix = 'ss' value = 'Sheet1' ).

* Table
rc_table = lc_document ->create_simple_element( name = 'Table' parent = rc_worksheet ).
r_table->set_attribute_ns( name = 'FullColumns' prefix = 'x' value = '1' ).
r_table->set_attribute_ns( name = 'FullRows' prefix = 'x' value = '1' ).

* Column Formatting (adjusting column width)
Width for column material number
rc_column = lc_document ->create_simple_element( name = 'Column' parent = rc_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '60' ).

Width for column Creation Date
rc_column = lc_document ->create_simple_element( name = 'Column' parent = rc_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '60' ).
Sharing Knowledge Formatted Excel in Email Attachment
9 | P a g e


Width for column Creation By
rc_column = lc_document ->create_simple_element( name = 'Column' parent = rc_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '60' ).

rc_column = lc_document ->create_simple_element( name = 'Column' parent = rc_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '60' ).


* Material Numbers
Creating Cell for row
rc_cell = lc_document ->create_simple_element( name = 'Cell' parent = rc_row ).

Assigning Style to define properties of cell
rc_cell ->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).

Providing value to cell to be displayed
rc_data = lc_document ->create_simple_element( name = 'Data' value = 'Material Number' parent = rc_cell ).
rc_data ->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).

* Creation Date
rc_cell = lc_document ->create_simple_element( name = 'Cell' parent = rc_row ).
rc_cell ->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
rc_data = lc_document ->create_simple_element( name = 'Data' value = 'Creation Date' parent = rc_cell ).
rc_data ->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).

* Created By
rc_cell = lc_document ->create_simple_element( name = 'Cell' parent = rc_row ).
rc_cell ->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
rc_data = lc_document ->create_simple_element( name = 'Data' value = 'Created By' parent = rc_cell ).
rc_data ->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).

* Material Type
rc_cell = lc_document ->create_simple_element( name = 'Cell' parent = rc_row ).
rc_cell ->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
rc_data = lc_document ->create_simple_element( name = 'Data' value = 'Material Type' parent = rc_cell ).
rc_data ->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).

Creating rows containing data from gt_final internal table
LOOP AT gt_final INTO gw_final.

*Column style
lv_style = 'Data'.

*Creating Row
rc_row = lc_document ->create_simple_element( name = 'Row' parent = rc_table ).

* Material Number
Creating Cell for a row
rc_cell = lc_document ->create_simple_element( name = 'Cell' parent = rc_row ).

Assinging attributes
rc_cell ->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = lv_style ).
Sharing Knowledge Formatted Excel in Email Attachment
10 | P a g e


Assinging value to variable
lv_value = gw_final-matnr.

Assinging value to cell
rc_data = lc_document ->create_simple_element( name = 'Data' value = lv_value parent = rc_cell ).

" Cell format
rc_data ->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).

* Creation Date
CLEAR lv_value.
rc_cell = lc_document ->create_simple_element( name = 'Cell' parent = rc_row ).
rc_cell ->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = lv_style ).
lv_value = gw_final-ersda.
CONDENSE lv_value NO-GAPS.
rc_data = lc_document ->create_simple_element( name = 'Data' value = lv_value parent = rc_cell ).
rc_data ->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).

* Created By
rc_cell = lc_document ->create_simple_element( name = 'Cell' parent = rc_row ).
rc_cell ->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = lv_style ).
lv_value = gw_final-ernam.
rc_data = lc_document ->create_simple_element( name = 'Data' value = lv_value parent = rc_cell ). " Data
rc_data ->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).


* Material Type
rc_cell = lc_document ->create_simple_element( name = 'Cell' parent = rc_row ).
rc_cell ->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = lv_style ).
lv_value = gw_final-mtart.
rc_data = lc_document ->create_simple_element( name = 'Data' value = lv_value parent = rc_cell ). " Data
rc_data ->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
ENDLOOP.


* Creating a Stream Factory
lc_streamfactory = lc_ixml->create_stream_factory( ).

* Connect Internal XML Table to Stream Factory
lc_ostream= l_streamfactory->create_ostream_itable( table = gt_xml_table ).

* Rendering the Document
lc_renderer = lc_ixml->create_renderer( ostream = lc_ostream document = lc_document ).
gv_rc = l_renderer->render( ).

* Saving the XML Document
gv_xml_size = l_ostream->get_num_written_raw( ).

ENDFORM. " process_xml_data
*&---------------------------------------------------------------------*
*& Form send_mail
*&---------------------------------------------------------------------*
Sharing Knowledge Formatted Excel in Email Attachment
11 | P a g e

* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM send_mail .

DATA: objpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE,
objhead LIKE solisti1 OCCURS 1 WITH HEADER LINE,
objbin LIKE solix OCCURS 10 WITH HEADER LINE,
objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE,
reclist LIKE somlreci1 OCCURS 5 WITH HEADER LINE,
doc_chng LIKE sodocchgi1,
tab_lines LIKE sy-tabix,
l_num(3),
subj_date(10) TYPE c.

If gt_material_e is not initial.

* Mail Subject
CONCATENATE sy-datum+6(2) '-' sy-datum+4(2) '-' sy-datum+0(4) INTO subj_date.
CONCATENATE 'Material Details ' subj_date INTO doc_chng-obj_descr SEPARATED BY space.

* Creation of the Document Attachment
LOOP AT gt_xml_table INTO gw_xml.
CLEAR objbin.
objbin-line = gw_xml-data.
APPEND objbin.
ENDLOOP.

*Number of line in excel
DESCRIBE TABLE objbin LINES tab_lines.

*Attachment Name
objhead = 'Material Details'.
APPEND objhead.

* Packing List For the E-mail Attachment
objpack-transf_bin = 'X'.
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = 1.
objpack-body_num = tab_lines.
CONCATENATE 'Material Detail' subj_date INTO objpack-obj_descr SEPARATED BY space.
objpack-doc_type = 'XLS'.
objpack-doc_size = tab_lines * 255.
APPEND objpack.

LOOP AT gt_material_e INTO gw_ material_e .
* Target Recipent
CLEAR reclist.
reclist-receiver = gw_sd_hedging-zemail.
reclist-rec_type = 'U'.
Sharing Knowledge Formatted Excel in Email Attachment
12 | P a g e

APPEND reclist.
ENDLOOP.

IF reclist IS NOT INITIAL.
* Sending the document
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = doc_chng
put_in_outbox = 'X'
TABLES
packing_list = objpack
object_header = objhead
contents_txt = objtxt
contents_hex = objbin
receivers = reclist
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
operation_no_authorization = 4
OTHERS = 99.
If sy-subrc EQ 0.
COMMIT WORK.
gv_mail_success = c_x.
Endif.

ENDIF.

Else.
gv_no_email = c_x.
Endif.
ENDFORM. " send_mail


Sharing Knowledge Formatted Excel in Email Attachment
13 | P a g e

About Author

Gursimran Singh Gujral is a SAP ABAP Consultant at Wipro Technologies. He has a Bachelor Degree in
Computer Science Engineering from Punjab Technical University, Punjab, India.
In case of any suggestion or feedback, feel free to write at er.gursimrangujral@gmail.com. Please
mention Sharing Knowledge in subject.

You might also like