CALL TRANSFORMATION to download Excel

By | Last Updated on August 3, 2015 | 22,709

Code snippet to show the usage of the ABAP Call Transformation to transform the data easily into the excel format by XML and download it.

Download EXCEL using CALL TRANSFORMATION

 
DATA: t_t100 TYPE STANDARD TABLE OF t100.
DATA: lv_xml  TYPE string.
DATA: lo_xml_doc TYPE REF TO cl_xml_document.
 
*
SELECT *
  FROM t100
  INTO TABLE t_t100
  UP TO 100 ROWS
  WHERE SPRSL EQ sy-langu.
 
*
CALL TRANSFORMATION id
   SOURCE data_node = t_t100
   RESULT XML lv_xml.
 
*
CREATE OBJECT lo_xml_doc.
lo_xml_doc->parse_string( lv_xml ).
lo_xml_doc->export_to_file( 'c:tempt100.xls' ).
 

Opening up the file in excel:

Excel_Open_XML_popup
Excel_Open_XML_popup_2
Excel_generated_by_XML_output

File as XML

Excel_as_XML

Remove VERSION from the File

 
DATA: t_t100 TYPE STANDARD TABLE OF t100.
DATA: lv_xml  TYPE string.
DATA: lo_xml_doc TYPE REF TO cl_xml_document.
 
*
SELECT *
  FROM t100
  INTO TABLE t_t100
  UP TO 100 ROWS
  WHERE SPRSL EQ sy-langu.
 
*
CALL TRANSFORMATION id
   SOURCE data_node = t_t100
   RESULT XML lv_xml.
 
*
REPLACE FIRST OCCURRENCE OF
    '<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">'
  IN lv_xml
  WITH '<asx:abap xmlns:asx="http://www.sap.com/abapxml">'.
 
*
CREATE OBJECT lo_xml_doc.
lo_xml_doc->parse_string( lv_xml ).
lo_xml_doc->export_to_file( 'c:tempt100.xls' ).
 

No more version column:

Excel_generated_by_XML_output_no_version

Do you have a Code Snippet which you want to share, Submit Code Snippet here

Share It!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

7 Comments

  • Rudolf Leye

    I don’t really like a processed XML object being serialized to string, worked over with text processing and being de-serialized back again to remove an XML attribute…

    Why not use the methods the way they are designed to be used:

     
    DATA: lv_xml2 TYPE REF TO if_ixml_document.
    lv_xml2 ?= ixml_factory-&gt;create_document( ).
    CALL TRANSFORMATION id
       SOURCE data_node = t_t100
       RESULT XML lv_xml2.
    lv_xml2-&gt;set_version( " ).
     
  • Hello Rudolf,

    I think I had tried to set the version by using the method SET_VERSION, but it sets the version of the XML document to blank, not the version within the asx:abap node.

    ?xml version=””?
    Due to this, the document wont open in excel.

    I even tried to parse the nodes from XML and tried to use the SET_VERSION column, but it actually leaves the VERSION tag as is but only replaces the value. Hence used the STRING and REplace.

    Thanks,
    Naimesh Patel

  • Mohit

    Hi Naimesh,

    How do we give custom column headings in this?

    Regards
    Mohit

  • Hello Mohit,

    I think using this ID transformation, it is not possible to change the column names. ID transformation is the identity transformation which is used for copying the source documents to target and vice versa. So, there is no space for any change.

    Thanks,
    Naimesh Patel

  • Siddharth

    Hi Naimesh,

    Thanks for creating this post.
    Is it possible to create an XML using a XML Schema file (.XSD) in SAP?
    I have an XSD file which is according to the requirements of a Partner System, and I need to generate a XML file using this schema.

    Thanks & Regards
    Siddharth

  • Rudolf Leye

    Most simple way would be to generate classes an transformations as described here:
    http://sapblog.rmtiwari.com/2012/10/power-of-core-using-xsd-for-xml.html

    Then you can generate a valid xml tha fulfils the specification given.

    Back to the other question:
    Though it is not possible to change anything using the id transform (as Naimesh correctly stated),
    you can of course write a simple xslt that does the job.

    In this case you even could use ST (Simple Transformations)

  • As Rudolf said, we are playing processed XML. But when no other ways look helpful, this trick does the work.. πŸ™‚

    Thanks Naimesh.. Exactly found what I needed today.. πŸ™‚

    Regards,
    Raju.

Comments on this Post are now closed. If you have something important to share, you can always contact me.