+ All Categories
Home > Documents > Formatted Excel in Email Attachment

Formatted Excel in Email Attachment

Date post: 22-Nov-2015
Category:
Upload: gursimran-singh-gujral
View: 109 times
Download: 12 times
Share this document with a friend
Popular Tags:
13
Formatted Excel in Email Attachment Sharing Knowledge Author: Gursimran Singh Gujral
Transcript
  • 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 * 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 *

  • 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 [email protected]. Please

    mention Sharing Knowledge in subject.


Recommended