Thursday 29 July 2021

Sending Spool List into Excel Format through Email Attachment

Requirement

I got one requirement where i need to call the SUIM transaction -> Roles by Complex Selection Criteria with multiple variants selections as background job and need to send the output in XLS format to multiple users maintained in distribution list.

There was one more additional requirement where i need to create single excel output with multiple tabs ( indicating each variant of report ).

Solution

This Solution will help for any report which we want to execute as background job and display the List View in Excel format.

Steps

1. Create one Custom report with Selection screen parameters as Report Name ( For which we need to display the result in excel), Distribution List ( where we need to send the file) and select option ( containing multiple variants of Report ).

2. Call the report and export the output in memory.

3. Get the report Output from memory by calling the FM ” LIST from MEMORY “.

4. Convert the list in ASCII using FM ” LIST TO ASCII “.

5. SPLIT the table into header and items according to the requirement.

6. Create the excel using IXML Factory methods with multiple tabs.

7. Finally attach the excel and send it to multiple distribution lists.

Step 1:

SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Prep
*** Image Added from the Test system as Dummy***


Step 2:


* Call report and export output in memory
submit (p_report)
using selection-set wa_vari-low
line-size sy-linsz
exporting list to memory and return.

Step 3:


    clear : list[].
* Get report output from memory
call function 'LIST_FROM_MEMORY'
tables
listobject = list
exceptions
not_found  = 1
others     = 2.

Step 4:


   clear : ascilines[].
* Convert it to ascii
call function 'LIST_TO_ASCI'
tables
listobject         = list
listasci           = ascilines
exceptions
empty_list         = 1
list_index_invalid = 2
others             = 3.

Step 5:


    loop at ascilines .
*   Skip separater lines
check ascilines+0(10) <> '----------' .
clear li_split .
split ascilines at '|' into table li_split .

if lines( li_split ) = 1 or li_split is initial.
if li_split is not initial.
wa_header = li_split[ 1 ].
endif.
append wa_header to gt_header.
clear : wa_header.
else.
if lv_flg <> abap_true.
append lines of li_split to gt_tab_head[].
lv_cnt = 1.
lv_flg = abap_true.
else.
unassign <fs_slip>.
loop at li_split assigning <fs_slip>.
append initial line to gt_items assigning field-symbol(<fs_item>).
<fs_item>-field = <fs_slip>-token.
<fs_item>-indx = lv_cnt.
unassign <fs_item>.
endloop.
lv_cnt = lv_cnt + 1.
endif.
endif.
endloop.

Step 6:


* Creating a ixml Factory
l_ixml = cl_ixml=>create( ).

* Creating the DOM Object Model
l_document = l_ixml->create_document( ).

* Create Root Node 'Workbook'
l_element_root  = l_document->create_simple_element( name = 'Workbook'  parent = l_document ).
l_element_root->set_attribute( name = 'xmlns'  value = 'urn:schemas-microsoft-com:office:spreadsheet' ).

ns_attribute = l_document->create_namespace_decl( name = 'ss'  prefix = 'xmlns'  uri = 'urn:schemas-microsoft-com:office:spreadsheet' ).
l_element_root->set_attribute_node( ns_attribute ).

ns_attribute = l_document->create_namespace_decl( name = 'x'  prefix = 'xmlns'  uri = 'urn:schemas-microsoft-com:office:excel' ).
l_element_root->set_attribute_node( ns_attribute ).

* Create node for document properties.
r_element_properties = l_document->create_simple_element( name = 'SUIM_REPORT'  parent = l_element_root ).
l_value = sy-uname.
l_document->create_simple_element( name = 'Author'  value = l_value  parent = r_element_properties  ).

* Styles
r_styles = l_document->create_simple_element( name = 'Styles'  parent = l_element_root  ).

* Style for Header
r_style  = l_document->create_simple_element( name = 'Style'   parent = r_styles  ).
r_style->set_attribute_ns( name = 'ID'  prefix = 'ss'  value = 'Header' ).

r_format  = l_document->create_simple_element( name = 'Font'  parent = r_style  ).
r_format->set_attribute_ns( name = 'Bold'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Interior' parent = r_style  ).
r_format->set_attribute_ns( name = 'Color'   prefix = 'ss'  value = '#FFFFFF' ).
r_format->set_attribute_ns( name = 'Pattern' prefix = 'ss'  value = 'Solid' ).

r_format  = l_document->create_simple_element( name = 'Alignment'  parent = r_style  ).
r_format->set_attribute_ns( name = 'Vertical'  prefix = 'ss'  value = 'Center' ).
r_format->set_attribute_ns( name = 'WrapText'  prefix = 'ss'  value = '1' ).

r_border  = l_document->create_simple_element( name = 'Borders'  parent = r_style ).
r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Bottom' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Left' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Top' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Right' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

***
* Style for tablename
r_style2  = l_document->create_simple_element( name = 'Style'   parent = r_styles  ).
r_style2->set_attribute_ns( name = 'ID'  prefix = 'ss'  value = 'Header1' ).

r_format  = l_document->create_simple_element( name = 'Font'  parent = r_style2  ).
r_format->set_attribute_ns( name = 'Bold'  prefix = 'ss'  value = '1' ).
r_format->set_attribute_ns( name = 'Color'  prefix = 'ss'  value = '#FFFFFF'  ).

r_format  = l_document->create_simple_element( name = 'Interior' parent = r_style2  ).
r_format->set_attribute_ns( name = 'Color'   prefix = 'ss'  value = '#4F81BD' ).
r_format->set_attribute_ns( name = 'Pattern' prefix = 'ss'  value = 'Solid' ).

r_format  = l_document->create_simple_element( name = 'Alignment'  parent = r_style2  ).
r_format->set_attribute_ns( name = 'Vertical'  prefix = 'ss'  value = 'Center' ).
r_format->set_attribute_ns( name = 'Horizontal'  prefix = 'ss'  value = 'Center' ).
r_format->set_attribute_ns( name = 'WrapText'  prefix = 'ss'  value = '1' ).
*
r_border  = l_document->create_simple_element( name = 'Borders'  parent = r_style2 ).
r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Bottom' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Left' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Top' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Right' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).
****

*   Style for header2
r_style4  = l_document->create_simple_element( name = 'Style'   parent = r_styles  ).
r_style4->set_attribute_ns( name = 'ID'  prefix = 'ss'  value = 'Header2' ).

r_format  = l_document->create_simple_element( name = 'Font'  parent = r_style4  ).
r_format->set_attribute_ns( name = 'Color'  prefix = 'ss'  value = '#FFFFFF'  ).

r_format  = l_document->create_simple_element( name = 'Interior' parent = r_style4  ).
r_format->set_attribute_ns( name = 'Color'   prefix = 'ss'  value = '#4F81BD' ).
r_format->set_attribute_ns( name = 'Pattern' prefix = 'ss'  value = 'Solid' ).

r_format  = l_document->create_simple_element( name = 'Alignment'  parent = r_style4  ).
r_format->set_attribute_ns( name = 'Vertical'  prefix = 'ss'  value = 'Center' ).
r_format->set_attribute_ns( name = 'Horizontal'  prefix = 'ss'  value = 'Center' ).
r_format->set_attribute_ns( name = 'WrapText'  prefix = 'ss'  value = '1' ).
*
r_border  = l_document->create_simple_element( name = 'Borders'  parent = r_style4 ).
r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Bottom' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Left' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Top' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Right' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).
***********

*Style for main title
r_style3  = l_document->create_simple_element( name = 'Style'   parent = r_styles  ).
r_style3->set_attribute_ns( name = 'ID'  prefix = 'ss'  value = 'Main' ).

r_format  = l_document->create_simple_element( name = 'Font'  parent = r_style3  ).
r_format->set_attribute_ns( name = 'Bold'  prefix = 'ss'  value = '1' ).
r_format->set_attribute_ns( name = 'Color'  prefix = 'ss'  value = '#366092' ).

******************
* Style for Data
r_style1  = l_document->create_simple_element( name = 'Style'   parent = r_styles  ).
r_style1->set_attribute_ns( name = 'ID'  prefix = 'ss'  value = 'Data' ).

r_border  = l_document->create_simple_element( name = 'Borders'  parent = r_style1 ).
r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Bottom' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Left' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Top' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Border'   parent = r_border  ).
r_format->set_attribute_ns( name = 'Position'  prefix = 'ss'  value = 'Right' ).
r_format->set_attribute_ns( name = 'LineStyle'  prefix = 'ss'  value = 'Continuous' ).
r_format->set_attribute_ns( name = 'Weight'  prefix = 'ss'  value = '1' ).

r_format  = l_document->create_simple_element( name = 'Alignment'  parent = r_style1  ).
r_format->set_attribute_ns( name = 'Vertical'  prefix = 'ss'  value = 'Center' ).
r_format->set_attribute_ns( name = 'Horizontal'  prefix = 'ss'  value = 'Left' ).

endif.

***** Tab 1 ****************************
* Worksheet(First tab)
clear : lv_value.
lv_value = lv_var.
r_worksheet = l_document->create_simple_element( name = 'Worksheet'  parent = l_element_root ).
r_worksheet->set_attribute_ns( name = 'Name'  prefix = 'ss'  value = lv_value ).

* Table
r_table = l_document->create_simple_element( name = 'Table'  parent = r_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
r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

r_column = l_document->create_simple_element( name = 'Column'  parent = r_table ).
r_column->set_attribute_ns( name = 'Width'  prefix = 'ss'  value = '150' ).

* Blank Row
r_row = l_document->create_simple_element( name = 'Row'  parent = r_table ).
r_row->set_attribute_ns( name = 'AutoFitHeight'  prefix = 'ss'  value = '0' ).
** Header of the Excel File
loop at lt_header into data(wa_header).
lv_value = wa_header-token.
r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
r_cell->set_attribute_ns( name = 'MergeAcross'  prefix = 'ss'  value = '3' ).
r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Main' ).
r_data = l_document->create_simple_element( name = 'Data'  value = lv_value  parent = r_cell ).
r_data->set_attribute_ns( name = 'Type'  prefix = 'ss' value = 'String' ).

r_row = l_document->create_simple_element( name = 'Row'  parent = r_table ).
r_row->set_attribute_ns( name = 'AutoFitHeight'  prefix = 'ss'  value = '0' ).

endloop.

clear : lv_value.

* Column Headers Row
r_row = l_document->create_simple_element( name = 'Row'  parent = r_table ).
r_row->set_attribute_ns( name = 'AutoFitHeight'  prefix = 'ss'  value = '1' ).

clear lv_value.
loop at lt_tab_head into data(wa_tab_head).
lv_value = wa_tab_head-token.
*Commodity
r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Header' ).
r_data = l_document->create_simple_element( name = 'Data'  value = lv_value  parent = r_cell ).
r_data->set_attribute_ns( name = 'Type'  prefix = 'ss' value = 'String' ).
endloop.

* Blank Row after Column Headers
r_row = l_document->create_simple_element( name = 'Row'  parent = r_table ).
r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Data' ).

r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Data' ).

r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Data' ).

loop at lt_items into data(wa_items).
if lv_idx <> wa_items-indx.
r_row = l_document->create_simple_element( name = 'Row'  parent = r_table ).
lv_idx = wa_items-indx.
endif.
clear : lv_value.
lv_value = wa_items-field.
r_cell = l_document->create_simple_element( name = 'Cell'  parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID'  prefix = 'ss'  value = 'Data' ).
r_data = l_document->create_simple_element( name = 'Data'  value = lv_value   parent = r_cell ).           " Data
r_data->set_attribute_ns( name = 'Type'  prefix = 'ss'  value = 'String' ).                               " Cell format
endloop.
clear : wa_items , wa_tab_head, wa_header, lv_idx.

Step 7:

  data :  email      type adr6-smtp_addr.
data: objpack   like sopcklsti1 occurs 2 with header line.
data: objhead   like solisti1 occurs 1 with header line.
data: objbin    like solix occurs 10 with header line.
data: objtxt    like solisti1 occurs 10 with header line.
data: reclist   like somlreci1 occurs 5 with header line.
data: doc_chng  like sodocchgi1.
data: tab_lines like sy-tabix.
data: l_num(3).
data: subj_date(10) type c.
* Creating a Stream Factory
l_streamfactory = l_ixml->create_stream_factory( ).

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

* Rendering the Document
l_renderer = l_ixml->create_renderer( ostream  = l_ostream  document = l_document ).
l_rc = l_renderer->render( ).

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

doc_chng-obj_descr = |SUIM User Details on Date : | && sy-datum+6(2) && |.| && sy-datum+4(2) && |.| && sy-datum+0(4).

describe table objtxt lines tab_lines.
read table objtxt index tab_lines.
doc_chng-doc_size = ( tab_lines - 1 ) * 255 + strlen( objtxt ).

* Packing List For the E-mail Body
objpack-head_start = 1.
objpack-head_num   = 0.
objpack-body_start = 1.
objpack-body_num   = tab_lines.
objpack-doc_type   = 'RAW'.
append objpack.

* Creation of the Document Attachment
loop at l_xml_table into wa_xml.
clear objbin.
objbin-line = wa_xml-data.
append objbin.
endloop.

describe table objbin lines tab_lines.
objhead-line = |SUIM User Details on Date : | && sy-datum+6(2) && |.| && sy-datum+4(2) && |.| && sy-datum+0(4).
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.
objpack-obj_descr = 'SUIM user Details'.
objpack-doc_type = 'XLS'.
objpack-doc_size = tab_lines * 255.
append objpack.

* Target Recipent
clear reclist.
reclist-receiver = p_dist.
reclist-rec_type = 'C'.
*  reclist-express = 'X'.
append reclist.

* Sending the document
call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'
exporting
document_data              = doc_chng
put_in_outbox              = 'X'
commit_work                = '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.
message 'Mail Sucessfully Sent' type 'S'.
endif.

Output: 


SAP ABAP Exam Prep, SAP ABAP Tutorial and Material, SAP ABAP Career, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Prep

*** Image Added from the Test system as Dummy***

Source: sap.com


No comments:

Post a Comment