Skip to end of metadata
Go to start of metadata

You can also use the abap2xlsx tool described in SDN blog abap2xlsx - Generate your professional Excel spreadsheet from ABAP, by Ivan Femia, which generates an XLSX file (available from Excel 2007). It is much more simple to use.

Exporting data to Excel - XML to the rescue

There is often a requierment to export report data to a Microsoft Excel spreadsheet.  If the report is a simple ALV grid then the ALV control can do it automatically, but otherwise ABAP coding is required.

The simplest solution is to download a tab separated file and then open this in Excel.  This is fine for simple data, but has lots of problems:

  • no formatting (headings, colours, column widths etc)
  • dates are YYYYMMDD, so are not shown as a date in Excel
  • Only one worksheet can be exported
  • leading zeros are lost
  • numeric fields of more than about 15 digits are truncated

There have traditionally been a few ways around this problem

  • Use SAP function modules to create an XLS file (no customisation possible)
  • Use OLE to start Excel and manipulate the data cell by cell (difficult, not much help, presentation server only)

However, Excel can now read and write xml files.  An ABAP program can create an xml file and it can be opened directly into Excel.  The advantages and possibilities are endless:

  • An xml file is plain text so can be created on the presentation server or application server;
  • alternatively, it can be sent as an attachment to an email, or can be provided as a parameter to a web service;
  • colours, fonts, number formats and so on can be specified;
  • formatting can be applied to rows, columns or single cells;
  • column heights and widths can be specified, or auto-formatted;
  • multiple worksheets can be created;
  • formulas can be entered in cells;

How to proceed

View and analyse xml file created by Excel

Create a new Excel workbook and enter the data you would like to see.  Add formatting, headings, sheet names etc, and some data. 

Save as an xml file.

Open the xml file in a text editor (eg Notepad).

It should look something like this:

Manual coding

Possibly the simplest way to create the XML is to create an internal table and fill it with the xml tags and data.

This code is from the ABAP forum, by Rudolf Lindenmann

Coding using the xml DOM concept

SAP provides an interface and classes for building up an xml document. This ensures that the xml document is well formed, ie does not contain any syntax errors. There is some overhead in the coding, so it is probably overkill for a really simple export, but it is a much better alternative for anything complex.
There are a number of blogs on creating an xml document using DOM.

Sample code for a report to download any database table to a file on the presentation server.
Column headings are provided from the data dictionary, formatted bold with yellow background, wordwrap.
Numeric fields are formatted with the number of decimal places specified in the data dictionary.
Text fields are formatted as Text rather than General

back to top

Coding using transformations

With ECC5 and later, xml can be created using Transformations. The transformation can be either coded with an ABAP-like syntax, or an XSLT style sheet.
For more information, look the F1 help on CALL TRANSFORMATION

Simple transformations

Export the file

Last but not least, the file needs to be exported. Options include:
Download to presentation server using GUI_DOWNLOAD
Download to application server using OPEN DATASET etc
Send as an attachment to an email
Have the xml table/string as a parameter on a RFC or web service.

back to top

  1. Guest

    Thank you, your article is a very helpful base to solve my problem.

    Can you tell me how to prevent the renderer from transforming :  & to <&>    ( to <(>     ) to <)>.

    I tried to call

      SPAN

    Unknown macro: { font-family}

    .L1S33

    Unknown macro: { color}

    .L1S52

    Unknown macro: { color}

      CALL METHOD l_renderer->SET_NO_ESCAPING
        EXPORTING NO_ESCAPING = '&'.

       SPAN

    Unknown macro: { font-family}

    .L1S33

    Unknown macro: { color}

    .L1S52

    Unknown macro: { color}

    CALL METHOD l_renderer->SET_NO_ESCAPING
        EXPORTING NO_ESCAPING = '('.

       SPAN

    Unknown macro: { font-family}

    .L1S33

    Unknown macro: { color}

    .L1S52

    Unknown macro: { color}

    CALL METHOD l_renderer->SET_NO_ESCAPING
        EXPORTING NO_ESCAPING = ')'.

    which did not work.

    The call with the three characters combined

       SPAN

    Unknown macro: { font-family}

    .L1S33

    Unknown macro: { color}

    .L1S52

    Unknown macro: { color}

    CALL METHOD l_renderer->SET_NO_ESCAPING
        EXPORTING NO_ESCAPING = '&()'.

    didn't work too.

    Regards

    Matthias MEvenkamp

  2. Guest

    thank you for this post. it was very useful to me. is there any limit on the number of records that can be read? I need to use this approach for a large number of records. Please reply!!

  3. Guest

    Great post.  One question though, when using the DOM to create the data in XLS format how could I include the following line in the output file:

    <?xml version="1.0"?>
    <?mso-application progid="Excel.Sheet"?>
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"..

     allowing Windows to automatically open the XML file into Excel when opened.

  4. Guest

    This is an extremely useful - and simple way to get an excel compatible file out of SAP - without the need for a presentation server - your code example / blog is very much appreciated!

  5. This is great post and helped me but it would have bigger size compared to xls in application server how to resolve that issue.

  6. Guest

    Regarding the <?mso-application progid="Excel.Sheet"?> comment.

    I found an alternativ way to insert this declaration to the XML. ( I did not manage to do it with the xml objects )

    I simple saved the XML to a string instead of a table and then I inserted the mso declaration.

    Then I converted the string back to table which I then download via the method cl_gui_frontend_services=>gui_download.

    Code: XML to string

    data l_xml_table_str type string.
    l_ostream = l_streamfactory->create_ostream_cstring( string = l_xml_table_str ).
    l_renderer = l_ixml->create_renderer( ostream  = l_ostream document = l_document ).
    l_rc = l_renderer->render( ).

    Code: Insert XML decl to string

    REPLACE FIRST OCCURRENCE OF '>' IN l_xml_table_str WITH '><?mso-application progid="Excel.Sheet"?>'.

  7. Guest

    Hi. Thanks for this article. The comment from Tobias Bengtsson helped me as well. Here is my snippet:


  8. Hi, That is a very helpful article.

    Do we have any options to merge cells and remove gridlines in excel throuhgh this approach?

  9. Hi,

    Can you please explain me how to create a hyperlink using this method???

     

    Please reply ASAP.......

  10. A Parsed processor instruction "<?mso-application progid="Excel.Sheet"?>" can also be added with using IXML API. Below delta:

    Code delta