Hi have created one excel file using XML code and sent it to mail as attachement. But when i open it, it displays error message - file is corrupted and cannot be opened.
below is my code - please review and tell what is wrong in it ASAP :
* 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 = 'TEST_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 = '#92D050' ).
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 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' ).
* Worksheet
r_worksheet = l_document->create_simple_element( name = 'Worksheet' parent = l_element_root ).
r_worksheet->set_attribute_ns( name = 'Name' prefix = 'ss' value = 'Sheet1' ).
* r_worksheet->set_attribute_ns( name = 'Protected' prefix = 'ss' value = '1' ). " WORKING
* 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 = '40' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '90' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '140' ).
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 = '90' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '90' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '90' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '90' ).
r_column = l_document->create_simple_element( name = 'Column' parent = r_table ).
r_column->set_attribute_ns( name = 'Width' prefix = 'ss' value = '90' ).
* Blank Row
r_row = l_document->create_simple_element( name = 'Row' parent = r_table ).
* 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' ).
* RFQ No.
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 = 'RFQ No.' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* RFQ Line Item No
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 = 'RFQ Line Item No.' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Material
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 = 'Material' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Quantity
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 = 'Quantity' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Order UNIT
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 = 'Order Unit' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Delivery Date
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 = 'Delivery Date' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* RFQ Creation Date
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 = 'RFQ Creation Date' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* RFQ Deadline Date
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 = 'RFQ Deadline Date' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* Price
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 = 'Net Price' parent = r_cell ).
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
** Login
* r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
* r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Header' ).
* CONCATENATE 'Login - ' lv_date+6(2) '/' lv_date+4(2) '/' lv_date+0(4) INTO l_value.
* r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ).
* r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ).
* 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' ).
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' ).
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' ).
* Data Table
LOOP AT it_final1 INTO wa_final1.
CLEAR l_value.
r_row = l_document->create_simple_element( name = 'Row' parent = r_table ).
* RFQ No.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
CLEAR l_value.
l_value = wa_final1-ebeln .
* CONDENSE l_value NO-GAPS.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* Line Item No
CLEAR l_value.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final1-ebelp.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'Number' ). " Cell format
* Material
CLEAR l_value.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final1-txz01.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* RFQ QTY
CLEAR l_value.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final1-ktmng.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'Number' ). " Cell format
* UNIT
CLEAR l_value.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final1-meins.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* DElivery Date
CLEAR l_value.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final1-eindt.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* RFQ Date
CLEAR l_value.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final1-aedat.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* RFQ Deadline
CLEAR l_value.
r_cell = l_document->create_simple_element( name = 'Cell' parent = r_row ).
r_cell->set_attribute_ns( name = 'StyleID' prefix = 'ss' value = 'Data' ).
l_value = wa_final1-agdat.
r_data = l_document->create_simple_element( name = 'Data' value = l_value parent = r_cell ). " Data
r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
* price
CLEAR l_value.
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 = l_value parent = r_cell ). " Data
* r_data->set_attribute_ns( name = 'Type' prefix = 'ss' value = 'String' ). " Cell format
ENDLOOP.
* 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( ).